sharke Posted December 23, 2009 Share Posted December 23, 2009 Hi guys, <?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 More sharing options...
Buddski Posted December 23, 2009 Share Posted December 23, 2009 <?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; ?> Link to comment https://forums.phpfreaks.com/topic/186176-calculate-sum-from-column/#findComment-983265 Share on other sites More sharing options...
sharke Posted December 23, 2009 Author Share Posted December 23, 2009 Correctly Thanks ! Link to comment https://forums.phpfreaks.com/topic/186176-calculate-sum-from-column/#findComment-983269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.