We help IT Professionals succeed at work.

PHP Pagination but limiting number of pages

katlees
katlees used Ask the Experts™
on
I found the attached code in EE and it works great, butthere are 115 pages limiting to 50 records per page.

How can I get it to be 1 2 3.....113, 114, 115

If I click on the third page, it would be
4, 5, 6 .....113, 114, 115
<table width="500" cellpadding="5">
     <tr bgcolor="#451403">
       <td><font color="#efcc83">Name &  Info</font></td>
       <td><font color="#efcc83">Charge</font></td>
     </tr>
<?php
$dblink = mysqli_connect('localhost', '*******', '********', '********');
$totalRows_rsWarrantResult = mysqli_fetch_row(mysqli_query($dblink,"SELECT COUNT(*) FROM Warrants"));
$totalRows_rsWarrantResult = $totalRows_rsWarrantResult[0];
 
//=================================================
$maxRows_rsWarrantResult = 50;
$pageNum_rsWarrantResult = 0;
if (isset($_GET['pageNum_rsWarrantResult'])) {
  $pageNum_rsWarrantResult = $_GET['pageNum_rsWarrantResult'];
}
$startRow_rsWarrantResult = $pageNum_rsWarrantResult * $maxRows_rsWarrantResult;
 
//
$totalPages_rsWarrantResult = ceil($totalRows_rsWarrantResult/$maxRows_rsWarrantResult)-1;
//-------------------------------------------------
 
 
$sql = "SELECT * FROM Warrants LIMIT $startRow_rsWarrantResult, $maxRows_rsWarrantResult";
$mysql_result = mysql_query($sql);
$last_lastname = "";
	$last_firstname = "";
	$last_state = "";
	$last_city = "";
	$last_DOB = "";
	
	while ($result_row = mysql_fetch_assoc($mysql_result))
	{
		$print_charge = $result_row['Charge'];

		if (strtolower($result_row['FirstName']) != $last_firstname ||
		    strtolower($result_row['LastName']) != $last_lastname ||
			strtolower($result_row['State']) != $last_state ||
			strtolower($result_row['City']) != $last_city ||
			$result_row['DOB'] != $last_DOB)
		{
			$print_name = '';
		
			if (!(empty($result_row['FirstName'])))
			{
				$print_name = $result_row['FirstName'];
			}
			
			if (!(empty($result_row['MiddleName'])))
			{
				if (empty($print_name))
				{
					$print_name = $result_row['MiddleName'];
				}
				else
				{
					$print_name .= " " . $result_row['MiddleName'];
				}
			}
			
			if (!(empty($result_row['LastName'])))
			{
				if (empty($print_name))
				{
					$print_name = $result_row['LastName'];
				}
				else
				{
					$print_name .= " " . $result_row['LastName'];
				}
			}
			
			$print_address = $result_row['Address'];
			$print_city = $result_row['City'];
			$print_state = $result_row['State'];
			$print_DOB = $result_row['DOB'];
		
			echo "<tr>\n";
			echo "<td valign=\"top\" align=\"left\" nowrap>$print_name <br />$print_address<br />$print_city, $print_state<br />$print_DOB</td>\n";
			echo "<td valign=\"top\" align=\"left\" nowrap>\n";
		
			
			$last_lastname = strtolower($result_row['LastName']);
			$last_firstname = strtolower($result_row['FirstName']);
			$last_state = strtolower($result_row['State']);
			$last_city = strtolower($result_row['City']);
			$last_DOB = $result_row['DOB'];
			
		}
echo "$print_charge<br />\n";


			
	}
echo "</td></tr>\n";
?>						
 </table>

                

<?php
for($i = 0; $i <= $totalPages_rsWarrantResult; $i++){
$display_number = $i+1;
if(($pageNum_rsWarrantResult) == $i){
echo "<font color=#FF9966>$display_number&nbsp;</font>";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_rsWarrantResult=$i\">$display_number</a>&nbsp;";
}
}
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You'd have to figure out how many rows are returned in total, so that the total number of pages can be determined; right now, it seems the creation of the list of pages is being created on-the-fly within the "while" loop.

Here's the idea:

Get the last 3 pages (lastPage, lastPage-1, lastPage-2).  Get the current page and the two surrounding it (currentPage, currentPage-1, currentPage+1).  If there is any overlap in the last & the first pages, no need for the middle set of dots.  If currentPage-1 is 1, then there are no prefix dots.
Most Valuable Expert 2011
Top Expert 2016

Commented:
I confess that I have not read the code snippet, and don't plan to, but I can point you to the canonical article on pagination.
http://www.sitepoint.com/perfect-php-pagination/
Software Developer
Commented:
Your last loop prints all pages:

<?php
for($i = 0; $i <= $totalPages_rsWarrantResult; $i++){
$display_number = $i+1;
if(($pageNum_rsWarrantResult) == $i){
echo "<font color=#FF9966>$display_number&nbsp;</font>";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_rsWarrantResult=$i\">$display_number</a>&nbsp;";
}
}
?>

Open in new window


As a general rule you should perhaps always offer first and last pages and a range including the current page, that means three ranges of pages in general, two ... inbetween.

<?php
$ellipsis="...";

for($i = 0; $i <= $totalPages_rsWarrantResult; $i++){
$display_number = $i+1;

If ($i>2 && $i<$totalPages_rsWarrantResult-2 && abs($i-$pageNum_rsWarrantResult)>1)
{ 
  echo $ellipsis;
   $ellipsis ="";
}
else
{
$ellipsis = "...";
if(($pageNum_rsWarrantResult) == $i){
echo "<font color=#FF9966>$display_number&nbsp;</font>";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?pageNum_rsWarrantResult=$i\">$display_number</a>&nbsp;";
}
}
}
?>

Open in new window


untested code.

Bye, Olaf.

Author

Commented:
Perfect.. Thank you!