This simple tutorial with show you how to display page navigation links with php/mysql much like the page navigation on chipmunk guestbook.
Okay once again like on all my other tutorials, the code comes first.
<?
$numentries=10;
//declares number of entries per page to display
global $start;
//declares start as a global variable
if(!isset($start))
{
$start=0;
//if start is not set, set it to zero
}
$newselect="Select * FROM table order by EntryID DESC LIMIT $start,$numentries";
//selects entries from start to number of entires after start
$newsquery=mysql_query($newselect) or die("dies");
while($news=mysql_fetch_array($newsquery))
{
print "$news[field]";
 }
$d=0;
$f=0;
$g=1;
//we use these three variables to keep track of counts
$r="SELECT*from table";
$r2=mysql_query($r);
while($order3=mysql_fetch_array($r2))
{
if($f%$numentries==0)
{
print "<A href='index.php?start=$d'>$g</a> ";
$g++;
}
$d=$d+1;
$f++;
}
?>
I know that was alot of code but I'll try ro explain it right here.
First off, I declared the number of entries I wanted to display per page at the top.
Then I declared my $start variable, which will be the variable I use to keep track what the number of the first entry on a page is. Start must be declared as a global.
The next if statement says that is start isn't set in the browser URL, set start to zero or page 1
Next we retrieve entries from what is defined in the brower URL as $start to however many entries we specified at numentries at the top of the code.
The while loop just prints out whatever the text is for those entries on the page we selected.
Next we declare d,f as variables and set them both to zero, I'll exaplain these as I explain the next loop, just know that they are needed.
Okay, now here's the hard part, the loop that actually displays the navigation.
First we select all the entries in that table, query it and convert it into the array. Now while there is an entry(this means for all entries in the array) this while loop runs.
The next line says that if the remainder of f divided by the number is zero, print a hyperlink with start set at $d. In the first case, it will print out a one, which means start is at zero, if you click on it, it just reloads the first page.
However, the f%numentires command tells it only to print another number when it equals zero so that means for it to print out a page 2 in the navigation, there must be more than $numentires entries in the sql table.
d increments each time so every time it print out a different number d will be a multiple of $numentires. The hyperlink then will take you to the page starting with the d(th) entry and each page is numentries long.
Okay thats the explanation and code for navigation numbers, if you still have questions please post them in the Support Forums
|