Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    201
    Thanks
    79
    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.
    Thanks.
    Last edited by conware; 06-26-2011 at 06:48 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,549
    Thanks
    8
    Thanked 1,095 Times in 1,086 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 ...
    index.php?page=7&action=next
    index.php?page=7&action=prev


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


    .

  • Users who have thanked mlseim for this post:

    conware (06-26-2011)

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

  • #4
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    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" />
    </form>
    if(isset($_POST['submit']))
    {

    session_start();
    // this sets variables in the session
    $_SESSION['host']='localhost';
    $_SESSION['user']='root';
    $_SESSION['pass']='';
    $_SESSION['year']='';
    $_SESSION['db']='sql';

    $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
    <?php
    $host = "localhost";
    $user = "root";
    $pass = "";

    if ($_SESSION['year'] == 2011 )
    {
    $db="ecct2011";
    }
    else
    {
    $db="ecct2012";
    }
    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
    •