I'm new to the forum, and have only just started web development on a professional basis.
I work for a small computer repair shop who have been doing websites for local businesses. I have a fair understanding of php and mysql as I have dabbled in amature development before.
The project I am working on at the moment is for a shop, but I came into the project about half way through so the database side was designed but the code work was not finished, so I decided to write my own scipt for the cart.
the problem i am having is; each product added to the basket, database side, is assigned a customer identifying number based on the session id number: $_SESSION['cust_id'] = session_id(); which is used when creating the recordset (SELECT * FROM baskets WHERE cust_id = $_SESSION['cust_id'] (a paraphrased copy of the SQL as the code is developed in dreamweaver). From this i can set a repeat region of the products which shows it's image, title, price, quantity,etc. But what I cant seem to do is get it to show the total.
The way I have done this is declaring $total =0; above the header of the page (I used to code java and as such tend to declare global variables befroe doign the <html> tag. When the price of the product is shown from the database have written $total = $total + $price; ($price is the price of the product... obv.) This line is within the do while loop for the repeated region. and doing an echo of $total as we go shows the right total when the echo is insidde the do while loop. But I want to display the total under the table of products and this echo of $total only shows the price of the last entry in the recordset.
This code is to display the price of the individual products in a table on the repeating region
Code: Select all
<?php
do { ?>
<tr>
<td><?php echo $row_cart['image']; ?></td>
<td><?php echo $row_cart['title']; ?></td>
<td><?php echo $row_cart['quant']; ?></td>
<td>
<?php
if($currency == 'UK')
{
echo '£';
}
else
{
echo '$';
}
$price = $row_cart['price']*$row_cart['quant'];
$price = number_format($price, 2, '.', ',');
echo $price;
$total = $total + $price;
echo $total;
?> </td>
<td><a href="cart/remove.php?basketid=<?php echo $row_cart['basket_id']; ?>">Remove from cart</a></td>
</tr>
<?php } while ($row_cart = mysql_fetch_assoc($cart)); ?>
Code: Select all
<?php
if($currency == 'UK')
{
echo '£';
}
else
{
echo '$';
}
$total = number_format($price, 2, '.', ',');
echo $total;
?>
I hope you can help with this problem.
Thanks
**EDIT**
I even tried to replace the adding of the price from within the do while loop and replace it with a method.
Code: Select all
$total =0;
function updateTotal($price)
{
$total = $total + $price;
return $total;
}