Jump to content

A link to next result in database?


Solarpitch

Recommended Posts

Hey,

 

I have a series of articles on my site that are being pull from the database. When the user select an article I want to have a "Next Article" button that will take them to the next article, then if there at the last article and they click next article it will simply bring them back to the first article again.

 

Here's what I was trying which works to some extent but not the full functionality.

 

<?php

// SNIPPET OF CODE

$sql = "select * from articles where article_id = ".$article_id."";
$result = mysql_query($sql) or die(mysql_error());
while(($row = mysql_fetch_row($result)) != false) 
	{

	$next_article = article_id + 1;

	if(!result)
	{
		$next_article= $story_id;
	}

                // ............... 

              <a href='articles.php?id=".$next_article."'></a>


?>

Link to comment
https://forums.phpfreaks.com/topic/93870-a-link-to-next-result-in-database/
Share on other sites

In a normal database with auto-incrementing ids, there's no guarantee that the 'next' and 'previous' numerical records would exist.

 

next higher is SELECT article_id from articles WHERE article_id>'$current_id' ORDER by article_id DESC LIMIT 1

 

next lower is SELECT article_id from articles where article_id<'$current_id ORDER by article_id ASC LIMIT 1

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.