Jump to content

[SOLVED] How to ... count total instances of a record and then sort the records ?


adrianTNT

Recommended Posts

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.

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'] . ")";

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.