Jump to content

[SOLVED] need help with simple for loop


ahs10

Recommended Posts

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

<?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 />';
}
?>

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

 

}

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.