Solarpitch Posted March 1, 2008 Share Posted March 1, 2008 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> ?> Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 1, 2008 Share Posted March 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.