Hi all,
I have two (related) questions, and I could really use some help!
I'm making a small system to browse documentation. All docs have a reference (1.1.1, 1.1.2, 2.5, 2.6.1 etc). One requirement is Prev/Next buttons to go to the next sub/section. I've stumbled on two problems though:
1: When hitting the "Previous" button (see very bottom), no matter what, you are always returned to the very first item - suggestions?
2: The "Next" button works fine - until you get to the last record, whereupon it (obviously) serves an entirely blank record, before going back to 1.1 - again, ideas to prevent this?
<?php
session_start ();
include ("common_db.php"); // DB Connectivity
SelectEntry($query);
$result = mysql_query ($query);
$row = mysql_fetch_array ($result);
function SelectEntry (&$query)
{
$ref = $_GET['page'];
$nav = $_GET['nav'];
$start = 1.1; /* Make sure this is set to the first item in the DB */
if (!$ref) { $ref = $start; } /* So if this is the first page, then serve the first page in the db. */
if ($nav == "next")
{
$query = "SELECT id, reference, title, content FROM `content` WHERE `reference` > '$ref' LIMIT 1;";
}
elseif (($nav == "last") AND ($ref != $start))
{
$query = "SELECT id, reference, title, content FROM `content` WHERE `reference` < '$ref' ORDER BY `reference` DESC LIMIT 1;";
}
else
{
$ref = $start;
$query = "SELECT id, reference, title, content FROM `content` WHERE `reference` = '$ref' LIMIT 1;";
}
}
// END SelectEntry
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// Show the content.
echo "<br>";
echo "ID: <b>".$row['id']."</b> - Reference <b>".$row['reference']."</b><br>";
echo "Title: <b>".$row['title']."</b><br>";
echo "Content: ".$row['content']."<br>";
echo "[ <a href='".$_SERVER['PHP_SELF']."?page=".$row['reference']."&nav=last'>Last</a> | ";
echo "<a href='".$_SERVER['PHP_SELF']."?page=".$row['reference']."&nav=next'>Next</a> ]";
?>