liamloveslearning Posted May 5, 2010 Share Posted May 5, 2010 Hi everyone, I have a looped region on my page where its calling all data from the db where 'x = user_id', Ive got all the data being called and displayed in a table, However I need to calculate a value of all the data in 1 of the columns. say I have 3 rows of data and they all have a field saying £3. its bringing them back as 1- product - 3.00 2- product - 3.00 3- product - 3.00 Is there a php function to output an addition of all the prices? Quote Link to comment https://forums.phpfreaks.com/topic/200834-total-of-looper-region/ Share on other sites More sharing options...
taquitosensei Posted May 5, 2010 Share Posted May 5, 2010 yourloophere { $total+=$price; } Quote Link to comment https://forums.phpfreaks.com/topic/200834-total-of-looper-region/#findComment-1053821 Share on other sites More sharing options...
liamloveslearning Posted May 5, 2010 Author Share Posted May 5, 2010 Sorry to sound naive, but ive tried to put it in my while loop and it breaks the syntax, is this incorrect? <?php do { ?> <tr> <td><?php echo $row_cart['id']; ?></td> <td><?php echo $row_cart['photo_id']; ?></td> <td><?php echo $row_cart['sess_id']; ?></td> <td><?php echo $row_cart['photo_name']; ?></td> <td><?php echo $row_cart['photo_price']; ?></td> </tr> <?php }while ($row_cart = mysql_fetch_assoc($cart)); { $total+=$row_cart['photo_price']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200834-total-of-looper-region/#findComment-1053863 Share on other sites More sharing options...
Muddy_Funster Posted May 6, 2010 Share Posted May 6, 2010 This would work better: <?php echo "<tr>" while ($row_cart = mysql_fetch_assoc($cart)) { echo "<td>".$row_cart['id']."</td>" echo "<td>".$row_cart['photo_id']."</td>" echo "<td>".$row_cart['sess_id']."</td>" echo "<td>".$row_cart['photo_name']."</td>" echo "<td>".$row_cart['photo_price']."</td>" $total = $total + $row_cart['photo_price']; } echo "</tr>" ?> the syntax error you had was a ; in the while line that shouldn't be there : <?php }while ($row_cart = mysql_fetch_assoc($cart)); { Quote Link to comment https://forums.phpfreaks.com/topic/200834-total-of-looper-region/#findComment-1053902 Share on other sites More sharing options...
liamloveslearning Posted May 6, 2010 Author Share Posted May 6, 2010 Brilliant, thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/200834-total-of-looper-region/#findComment-1053995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.