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.)

<?php
/* SNIPE.NET PAGE NUMBERING SNIPPET
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. 🙂 */

/* BEGIN CODE SECTION */
/* —————————————-*/

/* 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);
list(
$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) ) {
echo

;
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();
}

/* 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;
echo
“.$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 (
$real_page!=$page){
echo
” .$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 {
echo
“”.$real_page.“”;
}
}

/* 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;
echo
” .$page_name.$print_query.”page=“.$pagenext.””>Next > “;
}

}

/* END OF PAGE NUMBERING SNIPPET CODE */
/* —————————————-*/

?>

Advertisement

Themeforest
ssd-virtual-servers-banner-468x60
Previous post

Page Numbering (with x results per page)

Next post

Truncate text to x number of words

snipe

snipe

I’m a tech geek/dev/infosec-nerd/scuba diver/blacksmith/sword-fighter/crime fighter/ENTP/warcrafter/activist. I'm the CTO at Mass Mosaic and the CEO of Grokability, Inc. in San Diego, CA. Tweet at me @snipeyhead or read more...

  • rocoso

    nice! thanks!
    Its difficult to read with the orange on the tan background 🙂

  • rocoso

    nice! thanks!
    Its difficult to read with the orange on the tan background 🙂