SqlPager PHP Class
Download - and rename to SqlPager.php
This class make paging very simple - just provide it with the query to page, and it will do the rest. It uses the LIMIT keyword to do this. Currently, it support only MySQL.
Example
Basic Example
$pager = new SqlPager("SELECT name FROM Users WHERE status=1");
print $pager->getLink("first") . ' | ' . $pager->getLink("previous");
$pager->printPager();
print $pager->getLink("next") . ' | ' . $pager->getLink("last");
$this_page = $pager->getPage();
All Features Example
See this example in action
$pager = new SqlPager("SELECT url, title from Pages WHERE status=1");
$pager->printPager();
$this_page = $pager->getPage();
print "<br />";
print $pager->getLink("first") . '<br />' . $pager->getLink("previous");
$pager->printGoToInput();
$pager->printGoToDropDown();
print $pager->getLink("next") . '<br />' . $pager->getLink("last");
$pager->printItemsPerPageDropDown();
if($this_page) {
print "<ul>\n";
foreach($this_page as $row) {
print "<li><a href='$row[url]'>$row[title]</a></li>\n";
}
print "</ul>\n";
}
print $pager->getStatus();
Methods
- SqlPager
- getSql
- getPage
- printPager
- getLinkToPage
- getLink
- getStatus
- printItemsPerPageDropDown
- itemsPerPageChooser
- printGoToInput
- printGoToDropDown
- goToPage
SqlPager( $query[, $items_per_page = 10 ] )
Constructor
Arguments
- $query
-
The query that should be used for paging $items_per_page - The default number of items per page - defaults to 10
- $items_per_page
-
The Items Per Page
Optional Argument - if the argument is not provided, the function will use '10' as the default value.
SqlPager::getSql( )
Returns the SQL resource of the pager.
Returns
The SQL resource of the pager.
SqlPager::getPage( )
Returns all the items for one page in an array.
Returns
All the items for one page in a list.
SqlPager::printPager([ $return_data = false ] )
Prints the pager. Shows links to pages as numbers - except for the current page. The number of links to be shown is decided by the $opt['links_count'] member variable. If that is 0, all links are shown. If its 5, then 5 links are shown.
Arguments
- $return_data
-
The Return Data
Optional Argument - if the argument is not provided, the function will use 'false' as the default value.
SqlPager::getLinkToPage( $page )
Returns the link to the page of the given page number
Arguments
- $page
-
The page number of the page of which's link you want.
Returns
The url of the page - index.php?sp_page=2
SqlPager::getLink( $dir )
Return the Next/Previous and First/Last Link based on the argument. The link will be formated using the $link_template member variable of this class.
Arguments
- $dir
-
the direction of the link you want. Possible values are...
'next' get the next link
'previous' get the previous link.
'first' link to the first
'last' links to last page.
Returns
The HTML of the link - based on $link_template member variable
SqlPager::getStatus( )
Returns the status of the current page. Use the $status_template member variable as a template.
SqlPager::printItemsPerPageDropDown( )
Shows a Dropdown menu with values 5,10,20, 25, 50, 100. On selecting one option, that much items will be shown in one page.
SqlPager::printGoToInput( )
Input box based navigation. Shows a Input box where the users can type in a page number to go to that page number. Very useful for sites with a large number of pages.
SqlPager::printGoToDropDown( )
This function displays a dropdown box with all pages. Selecting a different page jumps to that page.