Solved

Recordset paging

Posted on 2014-02-23
9
298 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 51

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 51

Expert Comment

by:Julian Hansen
ID: 39881840
@Cathal - hang fire on that - shot from the hip without testing - fix on the way.
0
 
LVL 51

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

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 51

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 51

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now