Adding up numbers in array

Reply

Join Date: Sep 2009
Posts: 9
Reputation: Zack_G is an unknown quantity at this point 
Solved Threads: 0
Zack_G Zack_G is offline Offline
Newbie Poster

Adding up numbers in array

 
0
  #1
Sep 11th, 2009
Hi,

I am trying to find the sum of product's weight and can't seam to find anything that can help. I have included my code snippet below.


$totalweight = 0;
foreach($_SESSION['ddc']['productsincart'] as $key => $value) {
$sql = "SELECT * FROM ddcart_products WHERE id = '".$key."'";
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
//$totalweight = $totalweight+$data['weight'];

echo '<br>';
echo $data['name'];
echo '<br>';
echo 'Weight: '.$data['weight'];
echo '<br>';
echo 'Qty: '.$value;
echo '<br>';
$weight = $value * $data['weight'];
echo 'Total Weight: '.$weight;
echo '<br>';



}
How can i add up and display the total weight ($weight) for everything listed?

Thank's for your help.
Zack.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 922
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 130
darkagn's Avatar
darkagn darkagn is offline Offline
Posting Shark

Re: Adding up numbers in array

 
0
  #2
Sep 11th, 2009
You need to iterate through each of the table rows like so:
  1. $sql = "SELECT * FROM ddcart_products";
  2. $result = mysql_query($sql);
  3. $totalweight = 0;
  4. while($data = mysql_fetch_assoc($result))
  5. {
  6. echo '<br />';
  7. echo $data['name'];
  8. echo '<br />';
  9. echo 'Weight: '.$data['weight'];
  10. echo '<br />';
  11. echo 'Qty: '.$value;
  12. echo '<br />';
  13. $weight = $value * $data['weight'];
  14. echo 'Weight: '.$weight;
  15. echo '<br />';
  16. $totalweight += $weight;
  17. }
  18. echo "Total Weight = $totalweight<br />";
Last edited by darkagn; Sep 11th, 2009 at 10:50 am.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,110
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 68
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Adding up numbers in array

 
0
  #3
Sep 11th, 2009
It is probably easier to just use the SUM() function in Mysql:
http://dev.mysql.com/doc/refman/5.0/...functions.html

eg:

  1. SELECT *, SUM(weight) AS sum_weight FROM ddcart_products...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 434 | Replies: 2
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC