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
// Get from URL query string, or define, the current page # if ($_GET['page']) { $page = (int)$_GET['page']; } else { $page = 1; } ////////// // Define your pager preferences, how much to display per page ///////// // How many items per page $items_per_page = 14; ////////// // Build the pager query ///////// // Query the total amount of results to be used for pager. $sqla = "SELECT * FROM table"; $resulta = mysql_query($sqla); $num_rows = mysql_num_rows($resulta); $resulta = mysql_query($sqla); if(false === $resulta) { throw new Exception('Query failed with: ' . mysql_error()); } else { // free the result set mysql_free_result($resulta); } $page_count = 0; if (0 === $row_count) { // If no results from your query } else { // Calculate how many pages $page_count = (int)ceil($row_count / $items_per_page); // Return to first page if the page number is higher // than actual # of pages available if($page > $page_count) { $page = 1; } } //////// // Build our pager links /////// // The min and max number of pager links to display $min = max($page - 6, 1); $max = min($page + 6, $page_count); $pager = '<div class="pager">'; // 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 .= '<a class="nextprev" href="http://mywebsite.com?' . $query . '&page=' . $prev . '"><</a> '; } // 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 .= ' <a class="current-page">' . $i . '</a> '; // Display links to other pages } else { $pager .= '<a class="other-pages" href="http://mywebsite.com?' . $query . '&page=' . $i . '">' . $i . '</a> '; } } // Add a next page > link if($page < $page_count) { $next = ($page + 1); $pager .= '<a class="nextprev" href="http://mywebsite.com?' . $query . '&page=' . $next . '">></a> '; } $pager .= '</div>'; ////////// // 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
.pager { width: 100%; clear: both; margin: auto 0; text-align: center; font-size: 1.3em; padding-top: 25px; } a.other-pages { color: #ffffff; background: #b2dd4c; border-radius: 6px; padding: 6px 8px; } a.current-page { margin: 0 12px; } a.nextprev { font-weight: bold; margin: 0 20px; }

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.