How to Create a Simple Navigation Like Next-Previous with PHP

By | September 20, 2008

We often need to create something for navigation. For example, we have a gallery that consists of 400 pictures and we’d like do create 40 pages to list 10 pictures per page. In this sample we will need to create a navigation bar that will allow us to switch between pages easily. It is ok to have 40 pages, but if we have 4000? We will need to show, for example, 20 items per page. How to create such a navigation bar? Just follow the code below.

<?

// We’re requesting several parameters to build a navigation bar.
function bar($totalItems, $perPage, $currentPage, $pageName, $pageExtension, $start, $numarul) {

// $str will contain our navigation bar
$str = ”;
// If starting item is not defined, let’s take it’s the first
if (!$start) $start=1;
// If the total number of items (e.g. pictures) is bigger than the number you would like to have per page
if ($totalItems > $perPage)    {
// Let’s count how many items we need per page
$totalPages = $totalItems / $perPage;
// Taking an Integer Value
$totalPages = floor($totalPages+1);

// Determining the number of pages that are shown through the navigation bar
if (is_numeric($numarul)) $totPages=$numarul;
// Setting the default to 10 if something is wrong with out variable
else $totPages=10;

// Some arrangements to get the last page
if ($totalPages<$start+$totPages)
{
$ultimanoapte=$totalPages;
$ajunge=1;
}
else $ultimanoapte=$start+$totPages;

// If the page is not first, we need to create the link to previous pages (it will look like ” << ”
if ($start>1)
{
$previous=$start-$totPages;
if ($previous<1) $previous=1;

// All our urls will look like next-15.html
$str .= ‘<a href=”‘ . ‘next’ . ‘-‘ . $previous . ‘.’ . $pageExtension . ‘”>’ . ‘<<‘ . ‘</a> ‘;
}

for ($i=$start; $i<$ultimanoapte; $i++) {
if ($i == $currentPage) {
// Creating some spaces
$str .= $i . ‘ ‘;
continue;
}
// Adding the link to a page to our navigation bar
if ($i == $start) {
$str .= ‘<a href=”‘ . $pageName . ‘.’ . $pageExtension . ‘”>’ . $i . ‘</a> ‘;
} else {
$str .= ‘<a href=”‘ . $pageName . ‘-‘ . $i . ‘.’ . $pageExtension . ‘”>’ . $i . ‘</a> ‘;
}
}
// Creating a Next link (will look like ” >> ”
if (!isset($ajunge))    $str .= ‘<a href=”‘ . ‘next’ . ‘-‘ . $i . ‘.’ . $pageExtension . ‘”>’ . ‘>>’ . ‘</a> ‘;
}
// Returning the bar
return $str;
}
?>

What do we get in the sample? How will it look like?

2 3 4 5 6 7 8 9 10 11 12 13 14 15 >>

Here is the output of this function. Of course, we need to make these links working. In the sample I’ve used .htaccess file to add some strings:

RewriteRule ^main.html$  index.php [NC,QSA,L]
RewriteRule ^main-([0-9]*).html$  index.php?page=$1 [NC,QSA,L]
RewriteRule ^next-([0-9]*).html$  index.php?page=$1&next=$1 [NC,QSA,L]

So you can have just a single file named index.php to process all the request. You just need to call bar function will all your values (I think variable names are easy to understand). For example, you need to have this code:

if (isset($_GET[‘next’])) $nextPage=$_GET[‘next’];

This will catch the variable for bar function. Page names and extentions can be different; I’ve used “main” and “next” – you’ll need to change them in your .htaccess and your function.

You’re welcome to ask any questions in comments. The last thing I need to say that the code is adopted by me, but was previously created by Algasoft.

4 thoughts on “How to Create a Simple Navigation Like Next-Previous with PHP

  1. James

    Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.

  2. James

    Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.

  3. admin Post author

    I really don’t know how my blog appeared there, as it has nothing realted to black seo techniques, but… It means that my tricks are useful for all people :)

  4. admin Post author

    I really don’t know how my blog appeared there, as it has nothing realted to black seo techniques, but… It means that my tricks are useful for all people :)

Comments are closed.