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
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"; 

 

 

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.