Warptweet Posted February 16, 2007 Share Posted February 16, 2007 I used to have a flash system that it would use to display the latest 20 FILES in the /uploads directory. <?php $dir = 'uploads/pages/'; $H = opendir($dir); while (($file = readdir($H))!==false) { if ($file != '.' && $file != '..') { $a = explode('.', $file); if ($a[1]=='php') { $d = filemtime("$dir$file"); $res[$a[0]] = $d; } } } closedir($H); // sort by date desc arsort($res); // list 20 only $res = array_slice($res, 0, 20); foreach ($res as $f => $d) echo "<a href=\"http://www.warptweet.com/uploads/pages/$f.php\">$f</a><br>"; ?> BUT, now I have a compeltely new problem. I recently switched my website to use MySQL databases, and now it no longer uses files. Therefore, I need a way to convert this code so that it gets the latest 20 ROWS in the MySQL Database. There are two possibilites that can help you: I have two fields: flashid (an auto_increment field, so the latest flash uploaded has the highest flashidnumer) creationddate (a TIMESTAMP( field, which stores the date it was uploaded down to the second) Those two fields can help find which is the latest 20 flash stored in the MySQL Database, either by the latest date, or the highest flashid. I highly recommend using the highest flashid. How can I make this code get the latest 20 rows in my database and display the flashname column? Link to comment https://forums.phpfreaks.com/topic/38725-display-the-latest-20-mysql-rows/ Share on other sites More sharing options...
craygo Posted February 16, 2007 Share Posted February 16, 2007 use the query. I do not know your tablename so substitute yours for table1 <?php $sql = "SELECT * FROM table1 ORDER BY flashid DESC LIMIT 20"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ echo "<a href=\"http://www.warptweet.com/uploads/pages/".$r['flashname'].".php\">".$r['flashname']."</a><br>"; } ?> Ray Link to comment https://forums.phpfreaks.com/topic/38725-display-the-latest-20-mysql-rows/#findComment-186050 Share on other sites More sharing options...
Warptweet Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks a trillion! It works well! Oh, and one more thing... Would you mind trying to following... Display: the rows with their columns "flashrating" the highest (the highest 50 rows) Link to comment https://forums.phpfreaks.com/topic/38725-display-the-latest-20-mysql-rows/#findComment-186059 Share on other sites More sharing options...
Warptweet Posted February 16, 2007 Author Share Posted February 16, 2007 Would it be... <?php $sql = "SELECT * FROM flashid ORDER BY flashrating ASCD LIMIT 50"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ echo "<a href=\"http://www.warptweet.com/uploads/pages/".$r['flashname'].".php\">".$r['flashname']."</a><br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/38725-display-the-latest-20-mysql-rows/#findComment-186060 Share on other sites More sharing options...
craygo Posted February 16, 2007 Share Posted February 16, 2007 You want to go DESC because you want to start at the top. But yes you are right. Also it is ASC for ascending Ray Link to comment https://forums.phpfreaks.com/topic/38725-display-the-latest-20-mysql-rows/#findComment-186066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.