Jump to content

Display The Latest 20 MySQL Rows


Warptweet

Recommended Posts

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(8) 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

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

 

 

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

 

:D

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.