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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.