Jump to content

Count items from Sub-categories


sharke

Recommended Posts

Hi guys, i have function for count items from sub-categories but i need SUM from all sub-categories.

 

query.png

There is 233 items

 

<?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'];
?>

 

In this case i get result: 0

how can get correct result (233) items ?

 

Link to comment
https://forums.phpfreaks.com/topic/186054-count-items-from-sub-categories/
Share on other sites

soryy let me example, there is two tables (galerija and kategori)

table kategori:

kat_id

kat_parent

kat_naslov

kat_datum

 

table galerija:

pic_id

kat_id

and others columns...

 

If i use pasted code count only pictures from sub-categoryes, actually i need SUM all pictures from sub-categories. Similar like picture (233) items.

 

soryy for bad english

soryy but here no SUM

 

Category:

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

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

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

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

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

 

Table galerija columns:

pic_id

kat_id

etc....

Why you can't add SUM function for all subcategoris on existing code, also here we have how many photos have in subcategory, but not SUM for Category.

SELECT *, (SELECT COUNT(galerija.kat_id)  FROM galerija WHERE kat_id = kategori.kat_id) as cnt FROM kategori WHERE kat_parent = 1;

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.