sharke Posted November 20, 2009 Share Posted November 20, 2009 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/ Share on other sites More sharing options...
megaresp Posted November 20, 2009 Share Posted November 20, 2009 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; Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961914 Share on other sites More sharing options...
sharke Posted November 20, 2009 Author Share Posted November 20, 2009 this count only children, but i need count total items from Javascript and Ajax total items: Javascript = 8 Ajax = 3 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961934 Share on other sites More sharing options...
JustLikeIcarus Posted November 20, 2009 Share Posted November 20, 2009 Try this select category.name, count(*) from items, category where category.id = items.item_cat_id and category.name IN ('Javascript', 'Ajax') group by category.name Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961941 Share on other sites More sharing options...
sharke Posted November 20, 2009 Author Share Posted November 20, 2009 result: Resource id #16 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961955 Share on other sites More sharing options...
JustLikeIcarus Posted November 20, 2009 Share Posted November 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961960 Share on other sites More sharing options...
JAY6390 Posted November 20, 2009 Share Posted November 20, 2009 Could you paste the actual table structures? Are you trying to find the total number of items within the categories of Test 2 by only supplying test2's id? Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961970 Share on other sites More sharing options...
sharke Posted November 20, 2009 Author Share Posted November 20, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961980 Share on other sites More sharing options...
JustLikeIcarus Posted November 20, 2009 Share Posted November 20, 2009 Ok it must not like count(items.*) you can change the * to a specific col in the items table like count(items.id) if you have a column named id. Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961987 Share on other sites More sharing options...
JAY6390 Posted November 20, 2009 Share Posted November 20, 2009 Try this SELECT *, (SELECT COUNT(items.item_cat_id) as `cnt` FROM items WHERE item_cat_id = categories.id) FROM categories WHERE parent = 4 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-961992 Share on other sites More sharing options...
sharke Posted November 20, 2009 Author Share Posted November 20, 2009 JAY6390, JustLikeIcarus - i get this message -> Resource id #16 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962006 Share on other sites More sharing options...
JAY6390 Posted November 20, 2009 Share Posted November 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962007 Share on other sites More sharing options...
JustLikeIcarus Posted November 20, 2009 Share Posted November 20, 2009 While your testing queries you may find it easier to use phpmyadmins SQL form or a local mysql GUI app that will show you results Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962010 Share on other sites More sharing options...
sharke Posted November 20, 2009 Author Share Posted November 20, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962018 Share on other sites More sharing options...
JustLikeIcarus Posted November 20, 2009 Share Posted November 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962022 Share on other sites More sharing options...
JAY6390 Posted November 20, 2009 Share Posted November 20, 2009 it should do yea Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962026 Share on other sites More sharing options...
sharke Posted November 20, 2009 Author Share Posted November 20, 2009 strange, no errors anymore but in table items i have 1 query (items_cat_id (Javascript)), i get result = 0 Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962029 Share on other sites More sharing options...
sharke Posted November 21, 2009 Author Share Posted November 21, 2009 <?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). Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-962533 Share on other sites More sharing options...
fenway Posted November 24, 2009 Share Posted November 24, 2009 What's wrong with a standard LEFT JOIN? Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-964414 Share on other sites More sharing options...
sharke Posted November 24, 2009 Author Share Posted November 24, 2009 error ->Unknown column 'categories.id' in 'where clause' can you example with code ? Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-964424 Share on other sites More sharing options...
sharke Posted November 24, 2009 Author Share Posted November 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182287-count-items-from-subcategories/#findComment-964436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.