twsowerby Posted April 24, 2008 Share Posted April 24, 2008 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 Quote Link to comment Share on other sites More sharing options...
moselkady Posted April 24, 2008 Share Posted April 24, 2008 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"; Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 24, 2008 Share Posted April 24, 2008 agreed (beat me to it) Quote Link to comment Share on other sites More sharing options...
twsowerby Posted April 24, 2008 Author Share Posted April 24, 2008 awesome, thank you very much. Been working on this project for over 12 hours straight now, think my brains beginning to fry! Regards, Tom Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.