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.

Recommended Answers

All 2 Replies

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 />";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.