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
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

Link to comment
Share on other sites

this code i tested on phpmyadmin

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

 

Ok, then example with code how can i get SUM if i make column CNT or whatever ?

 

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.