I have got a table called articles in my database. I want to be able to extract the latest articles from there to my homepage. I tried a different number of array_reverse functions but I am not able to get them to work. Here is the code I have so far...
include('config.php');
$sql = "SELECT Id, Subject, Content FROM articles";
$result = mysql_query($sql, $db) or die(mysql_error());
while( $row = mysql_fetch_array($result))
{
$subject = $row["Subject"];
$content = $row["Content"];
$Id = $row["Id"];
echo <<<EOD
<div style="background-color:white; border-style: solid; border-width: thin; padding: 20px">
<a href="template.php?id=$Id">$subject</a><br>$content</div><hr>
EOD;
}
And this is one of the code I've tried which didnt work...
$posts = count(mysql_fetch_array($result));
for($i = $posts-1; $i>=0; $i--)
{
$subject = $row["Subject"];
$content = $row["Content"];
$Id = $row["Id"];
echo <<<EOD
<div style="background-color:white; border-style: solid; border-width: thin; padding: 20px">
<a href="template.php?id=$Id">$subject</a><br>$content</div><hr> EOD;
}
Also I want to the subjects of my links to be clickable so that when they are clicked they can go to a page where it will display the article according to it's ID. I have successfully implemented that by creating a template.php file and put a link "$subject" as my subject in the homepage. This works but I want to know if this is the correct way of doing this and if there are any better ways to do this...
Thanks and sorry if this has been asked, I did try looking for answers but didn't find anything that helped me.
Change your query to
$sql = "SELECT Id, Subject, Content FROM articles ORDER By Id DESC";
If you need only latest 5 articles then, add any limit for limiting results
$sql = "SELECT Id, Subject, Content FROM articles ORDER By Id DESC LIMIT 5";
Regards
iijb