Jump to content

Calculate SUM from column


sharke

Recommended Posts

Hi guys,

query.png

 

<?php
$result = mysql_query("SELECT *, (SELECT COUNT(galerija.kat_id)  FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1");
$total = mysql_fetch_assoc($result);

echo $total['cnt'];
?>

 

This code count how many items have in sub-categories, now i need calculate SUM of all items from subcategories.

 

Category:

- Test (kat_parent=0) - (kat_id=1)

--- test1 (kat_parent=1) - total photos = 126

--- test2 (kat_parent=1) - total photos =  62

================================

Total results on category Test = 188, i need this one...

Link to comment
https://forums.phpfreaks.com/topic/186176-calculate-sum-from-column/
Share on other sites

<?php
$total = 0;
$result = mysql_query("SELECT *, (SELECT COUNT(galerija.kat_id)  FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1");
if (mysql_num_rows($result) > 0) {
while ($data = mysql_fetch_assoc($result)) {
$total += $data['cnt'];
}
}

echo $total;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.