| | |
Adding up numbers in array
![]() |
•
•
Join Date: Sep 2009
Posts: 9
Reputation:
Solved Threads: 0
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.
How can i add up and display the total weight ($weight) for everything listed?
Thank's for your help.
Zack.
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>';
}
Thank's for your help.
Zack.
You need to iterate through each of the table rows like so:
php Syntax (Toggle Plain Text)
$sql = "SELECT * FROM ddcart_products"; $result = mysql_query($sql); $totalweight = 0; while($data = mysql_fetch_assoc($result)) { echo '<br />'; echo $data['name']; echo '<br />'; echo 'Weight: '.$data['weight']; echo '<br />'; echo 'Qty: '.$value; echo '<br />'; $weight = $value * $data['weight']; echo 'Weight: '.$weight; echo '<br />'; $totalweight += $weight; } 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. It is probably easier to just use the SUM() function in Mysql:
http://dev.mysql.com/doc/refman/5.0/...functions.html
eg:
http://dev.mysql.com/doc/refman/5.0/...functions.html
eg:
PHP Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- sort even and odd numbers from an array (C++)
- random numbers into an array (Java)
- Putting Single numbers in each array (C++)
- sort even and odd numbers for an array (C)
- Adding things to Array-Based Lists (C++)
- how do u find prime numbers in an array (C)
- C Program for Adding two numbers without using + sign (C)
- error while adding two numbers (C#)
Other Threads in the PHP Forum
- Previous Thread: Find random string between main string?
- Next Thread: Php Forum?
Views: 434 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api archive array arrays auto box buttons cakephp cart check checkbox class classes cms code combobox database date development directory display download dropdown dropdownlist drupal dynamic echo email error file files form forms functions header hosting href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu mlm mod_rewrite multiple mysql order parse password paypal php problem query radio recursion redirect regex remote rows script search select server session sort source sql string structure syntax table tutorial update updates upload url user validation variable video web website wordpress xml






