ballouta Posted October 20, 2008 Share Posted October 20, 2008 Hello I will try to describe my problem briefly. I have this table (Books) ID bname author version language isbn cat == ===== ==== ===== ====== ==== === 51 firstbok aut1 1 en-fr 9786915484 biography .... etc I have around 400 books with 21 categories I want to view these categories titles ONLY according to highest publications under these categories e.g.: Biography (145) History (130) etc... thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/129262-arranging-titles/ Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 try... SELECT `cat`,count(*) as num FROM `Books` GROUP BY `cat` ORDER BY num DESC Quote Link to comment https://forums.phpfreaks.com/topic/129262-arranging-titles/#findComment-670162 Share on other sites More sharing options...
ballouta Posted October 20, 2008 Author Share Posted October 20, 2008 waaw it is working I printed the categories and it is correct, but how do I print 'num' value in the while loop i am using? <?php $result = mysql_query(" SELECT `cat`,count(*) as num FROM `books` GROUP BY `cat` ORDER BY num DESC"); while( $row = mysql_fetch_array($result) ) { echo "$row[cat] () <br>"; } How i print number of books in each catgeory in the brackets? thank you Quote Link to comment https://forums.phpfreaks.com/topic/129262-arranging-titles/#findComment-670176 Share on other sites More sharing options...
wildteen88 Posted October 20, 2008 Share Posted October 20, 2008 How i print number of books in each catgeory in the brackets? Change your echo statement in your while loop too: echo $row['cat'] . ' ('.$row['num'].') <br>'; Quote Link to comment https://forums.phpfreaks.com/topic/129262-arranging-titles/#findComment-670187 Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 yeah, you should use single quotes around your array keys like wildteen88 describes. i like to do it this way (cleaner in my eyes): printf('%s (%s)<br />',$row['cat'],$row['num']); Quote Link to comment https://forums.phpfreaks.com/topic/129262-arranging-titles/#findComment-670188 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.