ahs10 Posted April 27, 2007 Share Posted April 27, 2007 i've tried many times to get this on my own and i can't. i have a table with four columns.... id, url, clicks, and title. i need to sort the rows by 'clicks' in descending order and get the data from the first five rows (the rows with the most clicks). i then need to output the data in a format similar to this.... url[1], title[1], clicks[1] <br> url[2], title[2], clicks[2] <br> url[3], title[3], clicks[3] <br> url[4], title[4], clicks[4] <br> url[5], title[5], clicks[5] <br> any help is much appreciated. thanks! Link to comment https://forums.phpfreaks.com/topic/49010-solved-need-help-with-simple-for-loop/ Share on other sites More sharing options...
per1os Posted April 27, 2007 Share Posted April 27, 2007 <?php $query = mysql_query("SELECT id,url,clicks,title FROM table_name ORDER BY clicks DESC LIMIT 5") OR DIE(mysql_error()); while ($row = mysql_fetch_assoc($query)) { print $row['url'] . ", " . $row['title'] . ", " . $row['clicks'] . '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/49010-solved-need-help-with-simple-for-loop/#findComment-240056 Share on other sites More sharing options...
sanfly Posted April 27, 2007 Share Posted April 27, 2007 in your query use "ORDER BY clicks DESC LIMIT 5" $r = mysql_query("SELECT * FROM mytable ORDER BY clicks DESC LIMIT 5"); $row = mysql_fetch_array($r); while($row = mysql_fetch_array($r)){ $url = $row['url']; $title = $row['title']; $clicks = $row['clicks']; // put the data into the table here } Link to comment https://forums.phpfreaks.com/topic/49010-solved-need-help-with-simple-for-loop/#findComment-240057 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.