Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Thanked 5 Times in 5 Posts

    Prev / Next links skip missing database records.

    Hi guys,

    Im trying to make my own blog. Currently I have coded it myself and combined it with phpbb.
    Now when someone clicks on a blog post I want to have links to the previouse / next posts.
    For example like how wordpress has it.
    But note I do not use wordpress I coded this myself so my forum members can comment with there accounts on the blog.

    Basically I know how to make a next and previouse link.
    But the problem is sometimes I'll need to remove a post ore deleted it.
    Now of course if I do this a gap will appear between posts.

    So for example this are the records in my database: 1 , 2 , 5 , 7

    Now the way I made my next and prev links was to connect to my database select the id and put that idea at the end of my $_GET variables.

    Like this:

    index.php?page=($row['id']+1); // next
    index.php?page=($row['id']-1); // prev

    Now everyone can see that this will not work when I remove a record in the database.

    If someone knows how I can solve this ore has a tip please let me know.
    Last edited by conware; 06-26-2011 at 06:48 PM.

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Cottage Grove, Minnesota
    Thanked 1,169 Times in 1,160 Posts
    You might get someone that recommends renumbering your table, don't do that.

    I would do something like this (but there are probably more ways to do it) ...

    The links on your page ...

    In each page, if the variable "action" exists, that means that they clicked "prev" or "next".
    If there is no "action" variable, then they want to display the page specified, so it works as normal.

    If they assigned a value to "action", then query it one way for "prev" and another for "next"

    Previous ID ...

    SELECT * FROM tablename WHERE id < $page ORDER BY id DESC LIMIT 1

    Next ID ...

    SELECT * FROM tablename WHERE id > $page ORDER BY id ASC LIMIT 1

    The query will result in only one row ... which will be the one before or after
    the specified page (id) ... depending on "action".


  3. Users who have thanked mlseim for this post:

    conware (06-26-2011)

  4. #3
    Regular Coder
    Join Date
    Mar 2010
    Thanked 5 Times in 5 Posts
    Thanks ones again mlseim, great code.
    This thread is solved lol.

  5. #4
    New to the CF scene
    Join Date
    Sep 2013
    Thanked 0 Times in 0 Posts
    hello sir i want take 2 database and connect only 1 database inserting values into text box...for eg. if i type 2011 in text box it should go to e2011 database or if i type2012 then it should go to e2012 database so can u do correction and send me plz

    1st page is index.php page
    <form action="" method="post">
    <input type="text" name="year" />
    <input type="submit" name="submit" value="Login" />

    // this sets variables in the session

    $con = mysql_connect($_SESSION['host'],$_SESSION['user'],$_SESSION['pass']) or die("Cannot Connect to Database Server");
    if (!$con)
    die('Could not connect: ' . mysql_error());
    mysql_select_db($_SESSION['db'],$con ) or die("database does not exist");

    next page is dbconn.php
    this page i m connecting to all page
    $host = "localhost";
    $user = "root";
    $pass = "";

    if ($_SESSION['year'] == 2011 )
    mysql_connect($host,$user,$pass) or die("Cannot Connect to Database Server");
    mysql_select_db($db) or die("database does not exist");


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts