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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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