adrianTNT Posted October 18, 2007 Share Posted October 18, 2007 I got stuck at something that I thought it was simple I have this code: do { echo $row_Recordset_count_referers['referer_url']; } while ($row_Recordset_count_referers = mysql_fetch_assoc($Recordset_count_referers)); That block will return site referers form database like this: some_site.com some_site.com some_other_site.com some_other_site.com some_other_site.com How can I display each unique record and total instances next to it (sorted descending)? so that it looks like this: site.com (4); other_site.com(2) foo_site(1) Can someone provide a code block? Or at least Instructions? Thank you. Link to comment https://forums.phpfreaks.com/topic/73779-solved-how-to-count-total-instances-of-a-record-and-then-sort-the-records/ Share on other sites More sharing options...
Wuhtzu Posted October 18, 2007 Share Posted October 18, 2007 It is quite simple <?php $getreferrers = mysql_query("SELECT referrer, COUNT(referrer) AS count FROM products GROUP BY referrer ORDER BY count DESC"); ?> This will allow you to do <?php while($row = mysql_fetch_array($getreferrers)) { echo $row['referrer'] . "(" . $row['count'] . ")"; } ?> Link to comment https://forums.phpfreaks.com/topic/73779-solved-how-to-count-total-instances-of-a-record-and-then-sort-the-records/#findComment-372249 Share on other sites More sharing options...
adrianTNT Posted October 18, 2007 Author Share Posted October 18, 2007 Thank you Wuhtzu I adjusted row names a bit and the code worked nicely. Have a great day. Link to comment https://forums.phpfreaks.com/topic/73779-solved-how-to-count-total-instances-of-a-record-and-then-sort-the-records/#findComment-372303 Share on other sites More sharing options...
Wuhtzu Posted October 18, 2007 Share Posted October 18, 2007 Since you did not give any field names I had to make them up, but I'm glad to hear it worked Link to comment https://forums.phpfreaks.com/topic/73779-solved-how-to-count-total-instances-of-a-record-and-then-sort-the-records/#findComment-372320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.