Part 8: Next and previous links

Originally posted by Michelle.

Please note: The Build-A-Blog series is an introduction to creating a simple blog script using PHP. These tutorials are meant to help you to learn PHP and MySQL and to use these to fetch and store data and display it on a web page. These tutorials should not be used ‘as is’ on a production website – especially if you are new to PHP and do not understand what you are doing. We would recommend that you try the B-A-B series on a safe, development environment – such as an offline installation of PHP and MySQL – so you can learn how everything works.

GWG and its staffers accept no responsibility for anything that may (or may not) happen to your site or server as a result of you using these tutorials – you do so AT YOUR OWN RISK.

In this instalment we will learn how to create next and previous links for easy navigation through our single entry pages. The following code should be placed on your single entry page (probably called journal.php or entry.php), just after your blog entry (and before any commenting functions). In this tutorial, the individual entry page will be referred to as journal.php.

MySQL should already be open on this page. Ensure that you have not closed MySQL previous to this code.

Now, the SQL query to find the id of the entry BEFORE the one you are viewing (ie, the previous id) should look like so:

$sql_prev = "SELECT * FROM php_blog WHERE id < '$id' ORDER BY id DESC LIMIT 1";

Remember that the $id variable comes from your url, and that any time you view journal.php, you should view it as journal.php?id=xx (where xx is an id number). The above code takes your $id variable, and find the first existing smaller id number — the id for the previous entry.

Next we’ll execute that SQL:

$result_prev = mysql_query ($sql_prev) or print ("Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error());

And while it is true, define $prev as the previous id:

while ($row = mysql_fetch_array($result_prev)) {
    $prev = $row['id'];
}

Now, in order to have a previous link if there is a previous entry, but not have one if no previous entry exists, we will use the following if statement:

if (isset($prev)) {
    // print a previous link
    printf("<a href=\"entry.php?id=%s\">Previous</a> -- ", $prev);
}
else {
    // just print the word "previous"
    print "Previous -- ";
}

So, the entire code for coming up with a previous link will look something like this:

$sql_prev = "SELECT * FROM php_blog WHERE id < '$id' ORDER BY id DESC LIMIT 1";
$result_prev = mysql_query ($sql_prev) or print ("Can't select previous entry id table php_blog.<br />" . $sql_prev . "<br />" . mysql_error());

while ($row = mysql_fetch_array($result_prev)) {
    $prev = $row['id'];
}

if (isset($prev)) {
    // print a previous link
    printf("<a href=\"journal.php?id=%s\">Previous</a> -- ", $prev);
}
else {
    // just print the word "previous"

    print "Previous -- ";
}

Our next link will be almost identical, except for finding the first existing LARGER id number, ie, the next entry id:

$sql_next = "SELECT * FROM php_blog WHERE id > '$id' ORDER BY id LIMIT 1";
$result_next = mysql_query ($sql_next) or print ("Can't select next entry id table php_blog.<br />" . $sql_next . "<br />" . mysql_error());

while ($row = mysql_fetch_array($result_next)) {
    $next = $row['id'];
}

if (isset($next)) {
    // print a next link
    printf("<a href=\"journal.php?id=%s\">Next</a>", $next);
}
else {
    // just print the word "next"

    print "Next";
}

And that’s all there is to it. If you have any trouble, post your entire journal.php code in the Tutorial Help forum.