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

LVL 58
GaryAsked:
Who is Participating?
 
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Julian HansenCommented:
@Cathal - hang fire on that - shot from the hip without testing - fix on the way.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.