Jump to content

View Previous and Next Databse Entry


HokieTracks

Recommended Posts

Hi, I currently have a database full of news entrys. Too display the latest news entry I use this code:

 

<?php

require('connect.php');


//Defines $query_photos as getting everything from photo
$query_news = "SELECT * FROM photo ORDER BY id DESC LIMIT 1";
//defines news as running the above
$news = mysql_query($query_news) or die(mysql_error());
$row_news = mysql_fetch_assoc($news);
//numbers rows as variable $totanews
$totalnews = mysql_num_rows($news);

//if there are no rows, then there is no news under that ID.
if ($totalnews == '0') {
echo '<script>alert("No news here under this id");</script>';
echo '<script>history.back(1);</script>';
}
//otherwise write the news title and underneath the news content
else {
echo $row_news['title'];?>
<br/><br/>
<?php
echo $row_news['content'];
?>
<?php }
?>

 

What I want to do now is create next and previous links that will take the user to the previous news entry or the next news entry in the database. Any ideas about how I can do this?

Link to comment
https://forums.phpfreaks.com/topic/153701-view-previous-and-next-databse-entry/
Share on other sites

Ignore the 'next' news article, if that code you have there displays the LATEST news, it'd be rather pointless.

 

I have no idea what to do, you would be needing to edit the top code, where it selects the article from the database.

 

Nobody seems to be replying on this forum much being probably because its easter. I really could do with help on my thread.

Ah, Yeah.

 

In the 'next' you'd need to add in an if statement to say that if there isn't any more news to display (user is at the most recent one) to kill the code.

 

I have no idea how to do it. Just focus around the part that gets the article, and work out how it gets it, like if its grabbing the time published so the most recent time published, it will display that article maybe.

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.