View Full Version : Powerfully Sorting
As5a5sIn5
08-28-2004, 05:22 PM
Okay on my site I have a hit counter that records date/time hit number(increment id) and IP....
is there anyway through PHP and SQL to sort the results so that it would just show how many times each IP visited rather then repeat printing the same IP for each time it visited...
Thanks In Advance!
Yes, but it's called grouping instead of sorting.
Your query could be:
$query="SELECT COUNT(*) visits, IP FROM table GROUP BY IP ORDER BY visits DESC";
this will return all IP's, sorted so that the one with the most visits comes first.
Note: 'visits' (the alias) is the name of the variable in the recordset for the visits.
As5a5sIn5
08-28-2004, 05:47 PM
Thanks Raf :D ! I should just PM you with all my questions :p
As5a5sIn5
08-28-2004, 06:01 PM
hmm still confused with that query...I pmed you with details Cuz I don't quite understand what COUNT does and why visits .. :confused:
dumpfi
08-28-2004, 07:45 PM
Shouldn't it be:
$query = 'SELECT COUNT(*) AS visits, IP FROM table GROUP BY IP ORDER BY visits DESC'; or is the AS not needed?
@ As5a:
COUNT(*) counts the rows where the IP-field holds the same value and visits is an alias for this value. So you can reference to the number of rows for each different ip by using visits.
dumpfi
As5a5sIn5
08-28-2004, 08:28 PM
thanks dumpfi...but how exactly do I refer to visits as it does not look like a variable...row[visits]? idk... :p
*Edit Also I can change 'visits' to what ever I want to ..to refer to it (after I learn how to refer to it :p ) right?
Please do not PM me for codingquestyions.
i indeed forgot the 'as' in the aliassing :(
anyway, since this seems hard to grasp, the complete code
$sql = "SELECT COUNT(*) AS visits, IP FROM table GROUP BY IP ORDER BY visits DESC";
$result = mysql_query($sql) or die ('Queryproblem');
if (mysql_num_rows($result) < 1){
echo 'There were no visits till now.';
}else{
while ($row = mysql_fetch_assoc($result)){
echo 'Number of visits for IP ' . $row['IP'] . ': ' . $row['visits'] . '<br />';
}
}
mysql_free_result($result);
chenge IP and table to your actual column and tablename
As5a5sIn5
08-29-2004, 10:57 AM
Thanks for explaining it guys! Works fine..Also sorry raf didn't know ya didn't like PM's :o :(
Thanks for explaining it guys! Works fine..Also sorry raf didn't know ya didn't like PM's :o :(
You're welcome. I don't like PM's for codingquestions. These should be solved 'in public' so that other people can also benefit from it...
As5a5sIn5
08-29-2004, 11:48 AM
You're welcome. I don't like PM's for codingquestions. These should be solved 'in public' so that other people can also benefit from it...
Hmm okay will remember maybe I'll PM you with links to my questions :p J/p
Hmm okay will remember maybe I'll PM you with links to my questions :p J/p
you'll quickly find yourself on my evergrowing ignore-list though :D
As5a5sIn5
08-29-2004, 01:16 PM
I said j/p (just playing) :(
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.