• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Recordset paging

I have the code below for some recordset paging.  But the php/logic of it has always annoyed me and looked messy to me.
Can anyone see a way to tidy it up.
$pages - is the total number of pages
$p - is the current page
There is only ever 5 pages shown to click through to and the current page is always shown in the middle of the navigation (where greater than 2) unless it is at the end of the navigation.

if($pages>1){
	echo'<ul class="pagination">';
	if($p>1){
		echo '<li><a href="'.$product_name.'-page-'.($p - 1) .'" rel="prev" class="previous"><<</a></li>';
	}
	$startpage=1;
	$endpage=5;
	
	if ($p>3 ){
		$endpage=$p+2;
		$startpage=$p-2;
	}
	if($startpage<1){$startpage=1; $endpage=5;}
	if($endpage>$pages){$endpage=$pages; $startpage=$pages-5;}
	if($startpage<1){$startpage=1; $endpage=5;}
	if($endpage>$pages){$endpage=$pages;}
	
	for ($paging=$startpage;$paging<=$endpage;$paging++){
		if($paging==$p){
			echo'<li class="active"><a>'.$paging.'</a></li>';
		}
		else{
			$paging==1?$temp_p="":$temp_p="-page-".$paging;
			$rel="";
			if($p==$paging-1){$rel=' rel="next"';}
			if($p==$paging+1){$rel=' rel="prev"';}
			echo'<li><a href="'.$product_name.$temp_p.'"'.$rel.'>'.$paging.'</a></li>';
		}
	}
	if($pages>$p && $pages>5){
		echo '<li><a href="'.$product_name.'-page-'.($p+1).'" rel="next" class="next">>></a></li>';
	}
	echo '</ul>';
}

Open in new window

0
Gary
Asked:
Gary
1 Solution
 
Lukasz ChmielewskiCommented:
Maybe this article will be any help to you:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_4742-Simple-PHP-pagination.html

This look a little messy. Perhaps you should move to some ready to use solutions like pagination classes ? Does the code work ? It will be hard to maintain, even if you tidy it up.
0
 
Julian HansenCommented:
It seems from your code that you want to do a sliding pagination. Not sure if this is better but might be useful
// Assumes
// $pages pre-calculated total pages 
// $p -> current page

// SET THE STARTPAGE TO THE MAX OF 1 AND CURRENT PAGE LESS 2
$startpage = max(1, $p-2);

// ENDPAGE IS THE MIN OF TOTAL PAGES AND CURRENT PAGE + 3
$endpage = min($pages, $p+3);

// START PAGINATION
echo "<ul>"; 

// IF STARTPAGE IS NOT THE FIRST PAGE THEN INCLUDE LINK TO PREVIOUS (AND FIRST IF REQUIRED)
if($startpage > 1) {
  // Link to prev
}
// DUMP THE PAGINATION
for($paging = $startpage; $paging < $endpage; $paging++) {

  // HANDLE THE CURRENT PAGE
  if ($paging==$p) {
    // Current page link
  }
  
  // THE REST
  else {
    // non current page link
  }
}

// IF MORE PAGES AFTER END PAGE SHOW A NEXT LINK
if ($endpage < $pages) {
  // Link to next
}

// AND CLOSE OFF
echo "</ul>";

Open in new window

0
 
Julian HansenCommented:
@Cathal - hang fire on that - shot from the hip without testing - fix on the way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Julian HansenCommented:
Here is a slightly modified fully working example
<!doctype html>
<html>
<head>
<style type="text/css">
ul {
  list-style: none;
}
ul li {
  display: inline-block;
  margin: 0 5px;
}
</style>
</head>
<body>
<?php
$pages = 10;
$p = empty($_GET['p']) ? 1 : $_GET['p'];
echo "p: $p<br/>";

// Assumes
// $pages pre-calculated total pages 
// $p -> current page

// SET THE STARTPAGE TO THE MAX OF 1 AND CURRENT PAGE LESS 2
$startpage = max(1, $p-2);
echo "Startpage: $startpage<br/>";

// ENDPAGE IS THE MIN OF TOTAL PAGES AND STARTPAGE + 4
$endpage = min($pages, $startpage + 4);
echo "Endpage: $endpage<br/>";

// START PAGINATION
echo "<ul>"; 

// IF STARTPAGE IS NOT THE FIRST PAGE THEN INCLUDE LINK TO PREVIOUS (AND FIRST IF REQUIRED)
if($startpage > 1) {
  echo '<li><a href="t560.php?p=' . ($p - 1) . '">&lt;&lt;</a></li>';
}
// DUMP THE PAGINATION
for($paging = $startpage; $paging <= $endpage; $paging++) {

  // HANDLE THE CURRENT PAGE
  if ($paging==$p) {
  echo '<li><span>' . $paging . '</span></li>';
    // Current page link
  }
 
  // THE REST
  else {
    // non current page link
  echo "<li><a href=\"t560.php?p=$paging\">$paging</a></li>";
  }
}

// IF MORE PAGES AFTER END PAGE SHOW A NEXT LINK
if ($endpage < $pages) {
  // Link to next
  echo '<li><a href="t560.php?p=' . ($p + 1) . '">&gt;&gt;</a></li>';
}

// AND CLOSE OFF
echo "</ul>";
?>
</body>
</html>

Open in new window

Fixes sliding window code and some typos
0
 
Ray PaseurCommented:
The canonical article on pagination is from SitePoint.  A bit old, but right on point.
http://www.sitepoint.com/perfect-php-pagination/
0
 
GaryAuthor Commented:
The sitepoint article is a bit naff to be honest
Roads, your logic seems a bit screwy, (ps look at SQL_CALC_FOUND_ROWS - silly to pull all records in a single query)

Julian your code seems to work fine and looks a lot cleaner than my code, will double check when I sober up/recaffinate myself in a short while.
0
 
Julian HansenCommented:
It took almost as long to read through the article and the code and try the sample as it did to write the pagination code (around 20 lines of code) above - and after that I am pretty sure the Paginated class does not support a sliding window pagination paradigm - which is what I understood you were needing.

@Cathal - what I usually do is instead of dumping the html directly - assign it to a string. That way I can include pagination top and bottom of the page on a single pagination call.

I put the code inside a function (pagination() strangely enough) and declare a static variable internally that is initialised to null. First call it is assigned the pagination string and then returns that string. Subsequent calls just return the string.
0
 
GaryAuthor Commented:
Works fine and looks cleaner.
0
 
Julian HansenCommented:
Awsome - thanks for the points.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now