Hello All,
I am fairly new to PHP ... I am trying to build a basic order form for a small wholesale database and am completely stuck.
The function of this page is display items and their price from the database along with an input box that will allow the user to enter an an order amount for each item. View The Page When the form is submitted I need to be able to multiply the Items price by the amount entered in the input box to get an item total. These totals will then be added together to get the Grand total.
The problem I have is twofold.
The Price Variable $ItemPrice is retrieved from the database using the command:
while($row=mysqli_fetch_array($result)){
echo '$ ' . $row['ItemPrice'] . '
}
As far as I can tell this only makes the Value of $ItemPrice available while the loop is being performed so when I run my "submit" script I cannot call the price with $ItemPrice.
// Check for Submission
if (isset($_POST['submit'])) {
// Grab the quantity from the POST
$ItemOrder = mysqli_real_escape_string($dbc, trim($_POST['ItemOrder']));
//Calculate and display the Item Total
$Price=$ItemPrice;
$LineTotal = $Price*$ItemOrder;
echo '<p class="info">The Line Total is: $' . $LineTotal . '</p>';
}
The Second Problem:
Since the items and prices in the order form are created dynamically I need to be able to create a unique set of variables for each item so instead of just referring to $ItemPrice I need to be able to preform the function of the submit button on all the items in the list based on each items price and the amount entered in the form. So that $ItemPrice1 would be multiplied by $ItemOrder1 and stored in $LineTotal1 and so forth so that I could then total all the $LineTotalx values and get a $GrandTotal
Here is the PHP Code
<?php
// Start the session
require_once('startsession.php');
// Insert the page header
$page_title = 'Place A New Order';
require_once('header.php');
require_once('appvars.php');
require_once('connectvars.php');
// Show the navigation menu
require_once('navmenu.php');
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Check for Submission
if (isset($_POST['submit'])) {
// Grab the quantity from the POST
$ItemOrder = mysqli_real_escape_string($dbc, trim($_POST['ItemOrder']));
//Calculate and display the Item Total
$Price=$ItemPrice;
$LineTotal = $Price*$ItemOrder;
echo '<p class="info">The Line Total is: $' . $LineTotal . '</p>';
}
else {
// No Amount Entered
echo '<p class="error">Please Enter Amount</p>';
$LineTotal = "0";
}
// Retrieve the product data from MySQL
$query="SELECT * FROM `items` ORDER BY 'ItemCat' ";
$result = mysqli_query($dbc, $query);
// Loop through the array of product data, formatting it as HTML
echo '<div id="form_box">';
echo ' <h1>Upper Crust Wholesale - Online Order</h1>
<p class="error">Please Complete the form below to place your order.</p><hr>';
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
while($row=mysqli_fetch_array($result))
{
// Display the product data
echo '<div class="product_line">';
echo '<div class="prod_name">' . $row['ItemName'] . ' / ' .$row['ItemCat'] . '</div>';
echo '<div class="prod_price">$ ' . $row['ItemPrice'] . ' ... per ... ' . $row['ItemAmount'] . ' ' . $row['ItemUnit'] . '' ;
?>
<label for="ItemOrder">Quantity:</label>
<input type="text" size="5" name="ItemOrder" value="<?php if (!empty($ItemOrder)) echo $ItemOrder; ?>" />
</div></div>
<?php
'</div>';
}
?>
<br />
<input type="submit" value="Update Total" name="submit" />
</form>
<?php
mysqli_close($dbc);
// Insert the page footer
require_once('footer.php');
?>
Thanks for looking I am open to any advice on making this approach work or even alternative ways of coding this type of page.