Snipe.Net Geeky, sweary things.

Page Numbering (with x results per page)

P

Although there are several examples of this type of code to be found online, I had never found one that easily did what I needed and was flexible enough that we only had to change a few things to use it again – so I wrote my own.

It links each page number (except for the current page number), prints Prev and Next links (if they need to be there), and also makes the current page number bold and not linked, like this:

Pages: < Prev 1 2 3 4 5 Next >

As you can see, the code is heavily commented to help you understand exactly what it is we’re doing – in the event that you actually give a damn. Further down the page, we also have the code for a google-like page number display, showing y page numbers per page. So without any further ado – check out the code.

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. (That’s why this code snippet page will give you an error if it executes – no database info.)

Page numbering variables
 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']);
		}

		// print out the actual item details - you would
		// change 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) ) {
			echo $foo;
		}

	// if there are no matches, print our an error
	} else {
		echo "No items listed";
	}

// if the query failed, lets see if mysql returns an error
} else {
	echo "An error has occurred: ";
	echo mysql_error();
}

This part below is the actual function that you call
from the page you want the page numbers to display on.

function make_user_page_nums($totalrows, $print_query, $page_name) {
	global $user_view_limit;
	global $page;
	global $limitvalue;

	echo "Pages: ";

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

	// get the total number of pages that are needed
	$numofpages = $totalrows/$user_view_limit;

	// loop through the page numbers and print them out
	for($i= 0; $i < $numofpages; $i++) {
		// if the page number in the loop is not
		// the same as the page we're on, make it a link

		$real_page = $i + 1;

		if ($real_page!=$page){
			echo " .$page_name.$print_query;
			echo "page=".$real_page."">".$real_page." ";

			// otherwise, if the loop page number is the
			// same as the page we're on, do not make it
			// a link, but rather just print it out
		} else {
			echo "".$real_page."";
		}
	} // end for loop

	// NEXT LINK - If the
	// (totalrows - $user_view_limit) * $page is
	// less 0 (meaning there is a remainder), print
	// the Next button.

	if(($totalrows-($user_view_limit*$page)) > 0){
		$pagenext = $page + 1;
		echo " .$page_name.$print_query."page=".$pagenext."">Next > ";
	}

} // end function
?>

About the author

snipe

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 @snipe.lol, 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 @snipe.lol, or read more...

Get in Touch