Jump to content

Count items from subcategories


sharke

Recommended Posts

Hi guys, i need help for count total items from subcategory.

 

table: items

items_cat_id

 

table: category

id | catname | parent

1    Test          0

2    PHP            1

3    Mysql          1

4    Test2          0

5    Javascrip    4

6    Ajax            4

----------------------------

explame, how can count Javascript and Ajax from Test2 ?

Link to comment
Share on other sites

I think you want a query that counts all the children of a specific category. If so, your query is...

 

select count(*) as tot_cats from category where parent='category.id';

 

In the above query, replace category.id with the actual id. For example, to count the number of sub-categories (children) of Test2, your query is...

 

select count(*) as tot_cats from category where parent='4';

 

If you want a count for every category with children, your query is...

 

select catname, count(*) as tot_cats from category where parent > '0' group by catname;

Link to comment
Share on other sites

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*) from items, category WHERE category.id = items.cat_id and category.' at line 1

 

Woops. try this

 

select category.name, count(items.*)

from items, category

where category.id = items.item_cat_id

and category.name IN ('Javascript', 'Ajax')

group by category.name

 

JAY6390 - i need this

total items:

Javascript = 8

Ajax = 3

 

result of total items from subcategories (Javascript and Ajax)

Link to comment
Share on other sites

Soryy i'm noob

i extract the data, but still get error: Notice: Undefined index: cnt in

 

<?php
$result = mysql_query("SELECT *, (SELECT COUNT(items.item_cat_id) as cnt FROM items WHERE item_cat_id = categories.id) FROM categories WHERE parent = 4");
$data = mysql_fetch_assoc($result);
echo $data['cnt'];

ok well thats jus the resource handle for the results. You now need to extract the data using mysql_fetch_assoc. I'm guessing you haven't worked with php & mysql much/at all

Link to comment
Share on other sites

If your going to do it that was the "as cnt" needs to come after the query in parenthesis like

SELECT *, (SELECT COUNT(items.item_cat_id)  FROM items WHERE item_cat_id = categories.id) as cnt FROM categories WHERE parent = 4"

 

Im not sure if doing it that way will bring back the result you want. it may.

Link to comment
Share on other sites

<?php
$result = ("SELECT *, (SELECT COUNT(items.item_cat_id)  FROM items WHERE item_cat_id = categories.id) as cnt FROM categories WHERE parent = 1");
while ($data = mysql_fetch_assoc($result)){
echo $data['cnt'];
}

now i get this result -> 000000100

 

In this category i have 9 sub-categories, i check items table from datebase how many query i have from those sub-categoris and i have 1 query from sub-category (7).

Link to comment
Share on other sites

your code working perfectly, it is was my fault

Thank you very much!

 

If your going to do it that was the "as cnt" needs to come after the query in parenthesis like

SELECT *, (SELECT COUNT(items.item_cat_id)  FROM items WHERE item_cat_id = categories.id) as cnt FROM categories WHERE parent = 4"

 

Im not sure if doing it that way will bring back the result you want. it may.

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.