Jump to content

[SOLVED] Help needed with php


twsowerby

Recommended Posts

Hi all,

 

I have the following code:

 

$viewselectsql = "SELECT admin_tracking.*, admin_tracking.userLoginID as user, admin_tracking.villaID as villa, count(admin_tracking.villaID) as idcount, userDetails.userClass as class FROM admin_tracking, userDetails
WHERE admin_tracking.userLoginID = userDetails.userLoginID AND userDetails.userClass = $class group by admin_tracking.villaID order by admin_tracking.villaID"; 
$query3 = mysql_query($viewselectsql);
while ($row3 = mysql_fetch_array($query3)) 
{
if ($row3["idcount"] >= 3)
{

echo "villa ".$row3["villa"]." viewed ".$row3["idcount"]." times by users in category $class <br />";
}
}

 

and the echo outputs:

 

villa 24 viewed 13 times by users in category 4

villa 27 viewed 16 times by users in category 4

villa 48 viewed 10 times by users in category 4

villa 51 viewed 9 times by users in category 4

villa 53 viewed 12 times by users in category 4

villa 54 viewed 6 times by users in category 4

 

How would I go about only echoing the top three highest "idcount" values? so instead of the above it would only show:

 

villa 27 viewed 16 times by users in category 4

villa 24 viewed 13 times by users in category 4

villa 53 viewed 12 times by users in category 4

 

Any help would be great!

 

Regards,

 

Tom

Link to comment
https://forums.phpfreaks.com/topic/102785-solved-help-needed-with-php/
Share on other sites

 

I think you need to change the order of your query to use idcount instead of villaID and make a limit to the result:

 

$viewselectsql = "SELECT admin_tracking.*, admin_tracking.userLoginID as user, admin_tracking.villaID as villa, count(admin_tracking.villaID) as idcount, userDetails.userClass as class FROM admin_tracking, userDetails
WHERE admin_tracking.userLoginID = userDetails.userLoginID AND userDetails.userClass = $class group by admin_tracking.villaID order by idcount desc limit 3"; 

 

 

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.