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.

$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.

Edited by mike_2000_17: Fixed formatting

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by digital-ether
0

You need to iterate through each of the table rows like so:

$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 />";

Edited by darkagn: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.