PHP - Page Navigator

The page navigator component written in php script. It contains 2 modes: Page Jump Style and Page Navigator style. It can set friendly url for navigator with some parameters, please see below.

Demo:

This demo set total records equal to 150 and set 10 items per page

Page Jump Style

Jump to page:


Page Navigator Style

• Simple Navigator
This style use javascript to set and post the value. This method can be used in general but not search engine friendly.


• Friendly Navigator
This style require url rewriter to handle valid target url. This method is more likely search engine friendly.

*Please note that the below link will not be a valid link because url rewriting is not set


• Partly Navigator
This style is useful when there are so many pages to navigate. Below the Interval is set to 5. Default interval is 3.


Download zip file: page_navigator.zip ( 6.48KB.)
Version 1.1 (Last update 12 Apr 2009)


How to setup:

Please note that you can instantiate one navigator and called print out so many as you want on current page

Instantiate Navigator

<?php
require_once('classes/tc_pageNav.php'); //include pageNav class

//suppose 150 records to be display, this actually get number from your database
$totalRecords = 150;

$page_nav = new tc_pageNav($totalRecords);
$page_nav->setPerPage(10);

$page_nav->calculate();

//don't forget to calculate before printout ?>

To instantiate navigator with startup page number please use

$page_nav = new tc_pageNav($totalRecords, 7); //set startup page to page 7

Page Jump Style

<?php
echo($page_nav->printNavJump()); //printout 'Page Jump Style'
?>

Page Navigator Style

• Simple Navigator

<?php
$page_nav->showInactiveNavigator(false);
$page_nav->setNavType(0);

echo($page_nav->printNavBar());
?> 

• Friendly Navigator

<?php
$page_nav->showInactiveNavigator(false);

$page_nav->setFriendlyUrl("page_navigator-%page-%var1.html"); //set friendly url format
$page_nav->setNavType(1); //set navigator type to friendly navigator

$page_nav->addSearchOption("var1", "test"); //tell the navigator to pass on variable var1
echo($page_nav->printNavBar()); //some of navigator url will be 'page_navigator-10-test.html'
?>

• Partly Navigator

<?php
$page_nav->showInactiveNavigator(false);
$page_nav->setPartInterval(5); //set interval for partly navigator

echo($page_nav->printNavBarPortion()); //print out partly navigator
?>

Functions

Constructor

tc_pageNav (int total_records)

Input the number of total records to navigate

tc_pageNav (int total_records, int startup_page)

Input the number of total records to navigate with startup page number

Methods

addSearchOption (string variableName, string variableValue)

To specify additional variable to be passed on navigator

Ex. $page_nav->addSearchOption("book_name", "php"); //tell the page navigator to pass on 'book_name' variable as 'php'

calculate ()

Calculate the navigator value before call print function. There are some variables need to be set by user before calling this function so make this function manually called.

Ex. $page_nav->calculate();

getCurrentPage ()

Get the current page of navigator

*This function should call after calculate() function

Ex. $page_nav->getCurrentPage(); //from the demo will return 0

getPageStart ()

Get the page start of navigator. In case of long navigator and navigator length specify in setNav() function, the page start might not be 1 but some others page.

*This function should call after calculate() function

Ex. $page_nav->getPageStart(); //from the demo will return 0

getPerPage ()

Get the page size that navigator used to calculate

*This function should call after calculate() function

Ex. $page_nav->getPerPage(); //from the demo will return 10

getMaxPage ()

Get the maximum page number that navigator is displayed

*This function should call after calculate() function

Ex. $page_nav->getMaxPage(); //from the demo will return 1

getMinPage ()

Get the minimum page number that navigator is displayed

*This function should call after calculate() function

Ex. $page_nav->getMinPage(); //from the demo will return 1

printNavBar ()

Print out navigator bar according to your setting in 'Simple' or 'Friendly' navigator

*This function should call after calculate() function

Ex. $page_nav->printNavBar();

printNavBarPortion ()

Print out navigator bar in according to your setting in 'Partly' navigator

*This function should call after calculate() function

Ex. $page_nav->printNavBarPortion();

printNavForm (string pagename)

Print out navigator form in 'Simple' navigator and 'Page Jump Style'. This function will automatically called by script.

*This function should call after calculate() function

Ex. $page_nav->printNavForm($_SERVER['PHP_SELF']); //print out navigator form target in current page itself

printNavJump ()

Print out navigator bar in 'Page Jump Style'

*This function should call after calculate() function

Ex. $page_nav->printNavJump();

printNext ()

Print out 'Next' link on navigator bar. This function will automatically called by script.

*This function should call after calculate() function

Ex. $page_nav->printNext();

printPrevious ()

Print out 'Previous' link on navigator bar. This function will automatically called by script.

*This function should call after calculate() function

Ex. $page_nav->printPrevious();

setFriendlyUrl (string url)

Set the friendly url for navigator according the following format.

To set the variable with the url, you can use '%' in front of variable. For example, to let navigator represent the page number on url, you can use '%page'. The navigator will match '%' and variable name you specify by addSearchOption() function and replace the value. This will require you to use Url Rewriting Technique to make the link url valid.

Format: %variableName

*This function should call before print out navigator

Ex. $page_nav->setFriendlyUrl('page_navigator-%page.html'); //some of url print out wil be 'page_navigator-6.html'

setNav (int length)

Set the navigator length on display. The hidden page navigator will show when the navigator is moved on. Default is 0 means show all navigators available.

*This function should call before calculate() function

Ex. $page_nav->setNav(10); //set the navigator length to show by 10

setPartInterval (int interval)

Set the interval of navigator display in 'Partly Navigator' mode. Default is 3.

*This function should call before calculate() function

Ex. $page_nav->setPartInterval(5); //set navigator to show in 5 items interval

setPerPage (int pagesize)

Set the item per page value use in calculating the total page navigate. Default is 30 items per page

*This function should call before calculate() function

Ex. $page_nav->setPerPage(50); //tell the navigator that 50 items will show on each page

setNavType (int type)

Set the navigator type. If type = 0 means 'Simple Navigator', type = 1 means 'Friendly Navigator'

*This function should call before print out navigator

Ex. $page_nav->setNavType(0);

showInactiveNavigator (bool show)

Set the navigator to show or hide inactive item (previous link or next link). Default is show

*This function should call before calculate() function

Ex. $page_nav->showInactiveNavigator(false); //set to false means not show inactive item