defonic Posted June 21, 2009 Share Posted June 21, 2009 Hello, first off I would like to say hello! I have a mySQL question regarding counting and getting a sum. I run a streaming cartoon video site, I paid a freelancer to make the script now he is MIA. I have a database table named 'categories' inside 'categories' is: id, title eg; (id)0001 (title)Comics, (id)0002 (title)Funnies Is there a way to count how many "id's" there under a specific title? I want to be able to display how many videos are in each category. Can someone help me with accomplishing this, or maybe tell me where to look? I am not looking for a free handout I am just trying to figure out where to begin. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/ Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 SELECT COUNT(*) cnt FROM categories GROUP BY title; Try that. Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860551 Share on other sites More sharing options...
pkedpker Posted June 21, 2009 Share Posted June 21, 2009 SELECT COUNT(*) cnt FROM categories GROUP BY title; Try that. Lol must be your brain fart again I think you met SELECT COUNT(*) AS cnt FROM categories GROUP BY title; not to be a asshole just helping you out Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860555 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 They're both the same. Both work. Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860559 Share on other sites More sharing options...
defonic Posted June 21, 2009 Author Share Posted June 21, 2009 Thanks for everyones help, I am new to SQL so heres what I have, I am trying to output it now like so: <?php include("includes/config.php"); $result=mysql_query("SELECT COUNT(*) AS cnt FROM category GROUP BY title;"); while($row=mysql_fetch_assoc($result)){ ?> <title><?=$row['title']; ?></title> <id><?=$row['id']; ?></id> <? } ?> Does not seem to work, am i doing something wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860563 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 Is the table category or categories? Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860566 Share on other sites More sharing options...
defonic Posted June 21, 2009 Author Share Posted June 21, 2009 the template is using smarty here is the line I have to reference: {foreach from=$array_categories item=category name=categ} <a href="./categ/{$category.id}/{$category.title}/" class="link"><b>{$category.title} I am assuming it is category Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860568 Share on other sites More sharing options...
Ken2k7 Posted June 21, 2009 Share Posted June 21, 2009 Uh.. no, completely wrong. You'll have to find where $array_categories is defined and from there, find the SQL it uses. For example - <?php $array = array(1,2,3,4,5); foreach ($array as $num) { // something here } item=category is like $num up there. It just represent the item. It doesn't tell you what the item is or how it was derived. Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-860642 Share on other sites More sharing options...
fenway Posted June 22, 2009 Share Posted June 22, 2009 I don't see why a simple count() and group by won't suffice. Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-861001 Share on other sites More sharing options...
defonic Posted June 22, 2009 Author Share Posted June 22, 2009 Ok thanks for everyone's help, again I am new at this and need to figure this out on my own since my programmer went MIA. After better research this is what I have. Table videos under the videos table I have id, title, date_added, category So the category is a number, so I may have "comic book #4, 76" 76 being a category. So i need to count how many 76 there are and so on. This is I need to output this in a simple fashion. Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-861060 Share on other sites More sharing options...
Ken2k7 Posted June 22, 2009 Share Posted June 22, 2009 SELECT COUNT(*) cnt FROM videos GROUP BY category; Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-861067 Share on other sites More sharing options...
defonic Posted June 22, 2009 Author Share Posted June 22, 2009 Yes this gives me an output off the count in each category, this is what I have. <?php include("includes/config.php"); $result=mysql_query("SELECT COUNT(*) cnt FROM videos GROUP by category;"); while($row=mysql_fetch_assoc($result)){ ?> <?=$row['cnt']; ?> videos in category <?=$row['category']; ?> <? } ?> <?=$row['category']; ?> above doesn't show up. How would I do 2 select statements to be able to capture 'category' I want it to display like "112 videos in category 72" Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-861408 Share on other sites More sharing options...
J.Daniels Posted June 22, 2009 Share Posted June 22, 2009 You need to return the category field: SELECT COUNT(*) cnt, category FROM videos GROUP BY category; Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-861518 Share on other sites More sharing options...
defonic Posted June 23, 2009 Author Share Posted June 23, 2009 Thanks for everyone's help! Quote Link to comment https://forums.phpfreaks.com/topic/163100-solved-counting-certain-entries-in-a-database/#findComment-861965 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.