Jump to content

Page Numbering


kool_samule

Recommended Posts

Hi Chaps,

 

I have a Query, that selects data from MySQL database:

 

$maxRows_rsShop = 20;
$pageNum_rsShop = 0;
if (isset($_GET['pageNum_rsShop'])) {
  $pageNum_rsShop = $_GET['pageNum_rsShop'];
}
$startRow_rsShop = $pageNum_rsShop * $maxRows_rsShop;

$colname_rsShop = "-1";
if (isset($_GET['shop'])) {
  $colname_rsShop = $_GET['shop'];
}
mysql_select_db($database_dbconnect, $dbconnect);
$query_rsShop = sprintf("
SELECT     tbl_shop.shop_title,                
                tbl_product.prod_id,                
                tbl_product.prodtitle,                
                tbl_product.prod_shop_thumb,                
                tbl_product.prod_price,                
                tbl_product_type.prod_type_title,                
                tbl_product_type.prod_type_plural,                
                SUM(tbl_product_type.prod_type_id) as quantity,
                img_name
FROM       tbl_shop_has_prod
INNER JOIN         tbl_shop                
                ON tbl_shop.shop_id=tbl_shop_has_prod.FK_shop_id
INNER JOIN         tbl_product                
                ON tbl_product.prod_id=tbl_shop_has_prod.FK_prod_id
INNER JOIN         tbl_product_type                
                ON tbl_product_type.prod_type_id=tbl_product.FK_prod_type_id
INNER JOIN         tbl_product_has_size                
                ON tbl_product_has_size.FK_prod_id=tbl_product.prod_id
INNER JOIN        tbl_web_img
                ON tbl_web_img.FK_shop_id=tbl_shop.shop_id
WHERE            shop_id=%s                
                AND prod_quantity > 0
GROUP BY         shop_id,                
                prod_id
ORDER BY        prod_added DESC", GetSQLValueString($colname_rsShop, "int"));
$query_limit_rsShop = sprintf("%s LIMIT %d, %d", $query_rsShop, $startRow_rsShop, $maxRows_rsShop);
$rsShop = mysql_query($query_limit_rsShop, $dbconnect) or die(mysql_error());
$row_rsShop = mysql_fetch_assoc($rsShop);

if (isset($_GET['totalRows_rsShop'])) {
  $totalRows_rsShop = $_GET['totalRows_rsShop'];
} else {
  $all_rsShop = mysql_query($query_rsShop);
  $totalRows_rsShop = mysql_num_rows($all_rsShop);
}
$totalPages_rsShop = ceil($totalRows_rsShop/$maxRows_rsShop)-1;


$queryString_rsShop = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rsShop") == false &&
        stristr($param, "totalRows_rsShop") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rsShop = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rsShop = sprintf("&totalRows_rsShop=%d%s", $totalRows_rsShop, $queryString_rsShop);

 

 

I have 'Move to Fist Page', 'Move to Previous Page', 'Move to Next Page', 'Move to Last Page' links that work OK, but what I'm after is page numbering links so the user knows how many pages there are, i.e.: 1, 2, 3.

 

Can anyone help / point me in the right direction?

 

Cheers

Link to comment
Share on other sites

Page numbering can be done relatively simply:

 

Here's a function that can be used for any page.

<?php

// This function Takes 3 Arguments - 1: The URL the linsk should point to. - 2: The maximum page number for these set of results - 3: The current page number
// Returns Array of Links (Minus the link for current page) of Pages.
function getPageNavList($link, $max_page_num, $current_page_num=1){ // Default page number is 1

// Set temporary number that we can modify for calculations
$i = $current_page_num;

// Also define a new array type variable that will hold an array of page numbers to be listed.
$pages = array();

// If we are on a page higher than 1, we should make all the page numbers from before this page.
if($i > 1){

	// This is where we make these first pages
	$i--; // We dont want to add the current page yet, start on the one before (could be 1 minimum)
	while($i > 0){
		array_unshift($pages, '<a href="'.$link.$i.'">'.$i.'</a>');
		$i--;
	}
}

// Now we can add the current page number to the pages array
array_push($pages, $current_page_num); // No need for a link here.

// And set the current page number back.
$i = $current_page_num;

// Check if this is the last page, if it isn't we would want to add all the pages after
if($i < $max_page_num){

	// This is where we make the pages after the curretn page.
	$i++; // We dont want to add the current page, we have already done that.
	while($i <= $max_page_num){
		array_push($pages, '<a href="'.$link.$i.'">'.$i.'</a>');
		$i++;
	}
}

return $pages;
}

// Make up some random numbers to test with:
$current_page = $_GET['page'];
$max_page = 10;

// This is the link to the current page - how it would look without the page number on the end.
$linktopage = "test.php?page=";

// Display the Links Very Easily:
echo(implode(" | ", getPageNavList($linktopage, $max_page,$current_page)));

?>

 

You can add functionality to it to show First/Last page links with a maximum of pages in the list (like google' search pagination).

 

-cb-

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.