Solved

Recordset paging

Posted on 2014-02-23
9
307 Views
Last Modified: 2014-02-24
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
Comment
Question by:Gary
9 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 39881791
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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39881821
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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39881840
@Cathal - hang fire on that - shot from the hip without testing - fix on the way.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39881870
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39882249
The canonical article on pagination is from SitePoint.  A bit old, but right on point.
http://www.sitepoint.com/perfect-php-pagination/
0
 
LVL 58

Author Comment

by:Gary
ID: 39882639
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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39882765
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
 
LVL 58

Author Closing Comment

by:Gary
ID: 39884390
Works fine and looks cleaner.
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 39884786
Awsome - thanks for the points.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question