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.

Dani AI

Generated

Short answer: compute each item's weight * qty and keep a running total, but avoid doing a separate DB query inside the loop and make sure numbers are cast and sanitized. already calculates the per-item total; is right to suggest accumulating a $total; 's idea of using SQL aggregation is also valid when the cart lives in the database.

A practical, safe PHP approach is to fetch all needed product rows in one query and then sum in PHP. Example (uses mysqli, sanitizes IDs and casts numeric values):

$cart = $_SESSION['ddc']['productsincart'] ?? [];
if ($cart) {
    $ids = array_map('intval', array_keys($cart));
    $sql = "SELECT id, weight FROM ddcart_products WHERE id IN (" . implode(',', $ids) . ")";
    $res = mysqli_query($conn, $sql);
    $totalWeight = 0.0;
    while ($row = mysqli_fetch_assoc($res)) {
        $id = (int)$row['id'];
        $qty = (int)($cart[$id] ?? 0);
        $totalWeight += (float)$row['weight'] * $qty;
    }
    echo number_format($totalWeight, 2);
}

If the cart is stored in a DB table, push the work to SQL for a single, efficient query (use prepared statements): for example, join product rows with the cart rows and SUM(p.weight * c.qty) to get the total on the server.

Extra tips: store weight in a numeric column (DECIMAL) and keep units consistent (grams or kg). Cast qty and weight before arithmetic to avoid surprises. Use number_format() or rounding for display. Avoid the old mysql_* extension—use mysqli or PDO with prepared statements to prevent injection and improve reliability.

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

It is probably easier to just use the SUM() function in Mysql:

eg:

SELECT *, SUM(weight) AS sum_weight FROM ddcart_products...
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.