Snipe.Net Geeky, sweary things.

Google Style Page Numbering (with x per page and y page numbers displayed)


With just a few modifications, we can create a piece of code that will not only give you x results per page with page numbers, but it will also allow you to specify how many page numbers should appear on the page at any time, much like Google.  (For example, if you have hundreds of page numbers, this would look messy and cluttered – using this code, you can tell it to only display 5 page numbers per page.)

Description: This code will allow you to provide item listings
as x per page, and will generate the Pages: 1 2 3, etc links
if needed. You will need to modify the queries as per your
own actual needs, and the database connection/selection code is
not included here, so it's assuming that code is already somewhere else
in your page. (Thats why this code snippet page will give you an error
if it executes - no database info.)

Don’t get freaked out by how long it is – 70% of it is comments to
help you understand what it is that we’re doing. 🙂 */

/* —————————————-*/

/* per page limit – this can be included in a seperate file, as long as you
are sure to include that fle on the page you want the numbering on – otherwise
its fine to just code it here – for this example, we’re using 16 items per page */
$user_view_limit = “16”;

/* set this variable to whatever the max number of page numbers you wish to be
displayed at any given time */
$max_pages_to_show = 5;

/* if there is no page # passed, assign $page the value of 1 */
if ((empty($page)) || ($page <= 0)){
$page = 1;

/* this code just figures out the limit for the sql statement that actually
gets that page’s item data */
$limitvalue = $page*$user_view_limit-($user_view_limit);

/* the query to get actual results – your query would go here, but be sure to
include the LIMIT $limitvalue, $user_view_limit part at the end.
Our example is pulling articles from the “articles” table that have the category ID of 2 */
$sql = “select Title from articles where CatID=2 LIMIT $limitvalue, $user_view_limit”;

/* the query to get the total number without the limit */
$sqlcount = “select count(*) from articles where CatID=2 “;

/* this is used by the function in case you need to pass other stuff in your
query string. If you’re not passing anything else, this should be set to just “?”
as is shown in this example –
To pass more variables through the query string, you would just change it to
something like: $print_query =”?cat_id=$cat_id&”; */
$print_query =“?”;

/* get the total number data and find out what the grand total is */
$sql_countresult = mysql_query($sqlcount);
$totalrows) = mysql_fetch_row($sql_countresult);

/* get the actual item data and print it out on the page */
if ($get_items = mysql_query($sql)) {
$num_items = mysql_num_rows($get_items);

/* see if we actually have any matches in the DB */
if ($num_items > 0) {

/* if theres more than one page needed, print out the page #s
In this example, products.php is the page that the link will be printed out with.
To use a different page, simply change this value in your function call */
if ($user_view_limit < $totalrows) {
make_user_page_nums($totalrows, $print_query, $_SERVER[‘PHP_SELF’], $user_view_limit, $page, $max_pages_to_show);

/* print out the actual item details – you would cange this code to
make it print out the fields and data the way you want it to appear
on the page */
while (list($foo) = mysql_fetch_row($get_items) ) {



/* if there are no matches, print our an error */
} else {
“No items listed”;

/* if the query failed, lets see if mysql returns an error */
} else {
“An error has occurred:

/* THE ACTUAL make_user_page_nums FUNCTION */
/* —————————————-*/

function make_user_page_nums($total_results, $print_query, $page_name, $results_per_page, $page, $max_pages_to_show) {

echo “Pages: “;

/* PREV LINK: print a Prev link, if the page number is not 1 */
if($page != 1) {
$pageprev = $page 1;
“.$page_name.$print_query.”page=“.$pageprev.””><Prev “;

/* get the total number of pages that are needed */

$showpages = round($max_pages_to_show/2);
$numofpages = $total_results/$results_per_page;

if ($numofpages > $showpages ) {
$startpage = $page $showpages ;
} else {
$startpage = 0;

if ($startpage < 0){
$startpage = 0;

if ($numofpages > $showpages ) {
$endpage = $page + $showpages;
} else {
$endpage = $showpages;

if ($endpage > $numofpages){
$endpage = $numofpages;

/* loop through the page numbers and print them out */
for($i = $startpage; $i < $endpage; $i++) {

/* if the page number in the loop is not the same as the page were on, make it a link */
$real_page = $i + 1;
if (
” .$page_name.$print_query.”page=“.$real_page.””>”.$real_page.” “;

/* otherwise, if the loop page number is the same as the page were on, do not make it a link, but rather just print it out */
} else {

/* NEXT LINK -If the totalrows – $results_per_page * $page is > 0 (meaning there is a remainder), print the Next button. */
if(($total_results-($results_per_page*$page)) > 0){
$pagenext = $page + 1;
” .$page_name.$print_query.”page=“.$pagenext.””>Next > “;


/* —————————————-*/


About the author


I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at, or read more...

By snipe
Snipe.Net Geeky, sweary things.

About Me

I'm a tech nerd from NY/CA now living in Lisbon, Portugal. I run Grokability, Inc, and run several open source projects, including Snipe-IT Asset Management. Tweet at me @snipeyhead, skeet me at, or read more...

Get in Touch