Jump to content

News in decending order?


mrgrim333

Recommended Posts

So this is what I've got so far.

$getnews = mysql_query("SELECT MAX(id) FROM posts");

if (!$getnews) {
    die('Could not connect: ' . mysql_error());
}

for($i = 0;$i<5;$i++)
{
         $row = mysql_fetch_assoc($getnews);
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
$name = $row['name'];

echo "<table width=500>
<tr><td bgcolor=#FFFFFF colspan=2>
<b><center>$title</b></center></td></tr>
<tr><td colspan=2 bgcolor=222222>
<div align=left><font color=#FFFFFF><b> $name</b> posted this $date</font></div>
</td></tr>

<tr><td valign=top align=left width=75><img src=images/seth.jpg><br></td><td align=left valign=top >";

echo nl2br($body);

echo "<BR><BR></td></tr></table>";
}

 

It's not working  :confused:

Link to comment
https://forums.phpfreaks.com/topic/175835-news-in-decending-order/
Share on other sites

Your query is only selecting MAX(id), which won't help you if you're trying to display other information.

 

You should let your query handle the ordering and limiting, you should do something like this:

 

$getnews = mysql_query("SELECT * FROM posts ORDER by `id` DESC LIMIT 5");

if (!$getnews) {
    die('Could not connect: ' . mysql_error());
}

while($row = mysql_fetch_assoc($getnews))
{
     echo "<table width=500>
<tr><td bgcolor=#FFFFFF colspan=2>
<b><center>{$row['title']}</b></center></td></tr>
<tr><td colspan=2 bgcolor=222222>
<div align=left><font color=#FFFFFF><b> {$row['name']}</b> posted this {$row['date']}</font></div>
</td></tr>

<tr><td valign=top align=left width=75><img src=images/seth.jpg><br></td><td align=left valign=top >";

echo nl2br($row['body']);

echo "<BR><BR></td></tr></table>";
}

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.