Create A Pager For Your Content
Here's a little script if you want to add pagination to your mysql query output using PHP. You can see the pager in action on our Templates page.
This pager will allow you to:
- define how many items you want to display per page
- set how many pager links should be shown, which is useful if you have a lot of pages
- position the pager anywhere you want on the page (above content, below content or both)
- fixes the url query string so that you don't end up with mulitple instances of &page= as users navigate your site
- adds classes to the pager links so that you can style the links using CSS
';
// Remove page from query string, which is otherwise concating multiple &page= query values to URL string
$query = preg_replace('/&page=\\d+/', '', $_SERVER["QUERY_STRING"]);
// Add a previous page < link
if($page > 1) {
$prev = ($page - 1);
$pager .= '< ';
}
// Display the defined number of previous and next links on either side of the current page
for($i = $min; $i <= $max; ++$i) {
// Display our current page link
if ($i === $page) {
$pager .= ' ' . $i . ' ';
// Display links to other pages
} else {
$pager .= '' . $i . ' ';
}
}
// Add a next page > link
if($page < $page_count) {
$next = ($page + 1);
$pager .= '> ';
}
$pager .= '
';
//////////
// Build content display query, to include pager limits
/////////
$num_rows = 0;
// Prepare the offset for mysql display query
$offset = ($page - 1) * $items_per_page;
$sql = "SELECT id FROM table";
$sql .= " LIMIT " . $items_per_page . " OFFSET " . $offset . "";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
///////
// Content Display
//////
// If you want to display pager links above content
echo $pager;
if (!empty($num_rows)) {
while ($obj = mysql_fetch_object($result)) {
// Display your content results
// ie: echo $obj->id;
}
}
// If you want to display pager links below content
echo $pager;
?>
And here's the css to help you style your pager
Sticky Side Column
If you're using our PHP Website Template, this is the optional side bar column you can add to some or all of your pages. It can be useful for adding advertisements such as Adsense ads to be displayed on multiple pages.
When you include the side column, your main content will automatically adjust its width to accommodate it.
The side bar is 'sticky'; it will scroll down and remain in view.
You can turn on/off the side bar column per page by defining the $showSide option located at the top of each file.