PHP Pagination

Soldato
Joined
22 Aug 2005
Posts
8,969
Location
Clydebank
Hi there - was having probs eariler with MysQL iming out and stuff, but as I progressed I foudn the way, and now I only query what I'm using per page.

I wrote this code for pagination, but is it possible to reverse the logic, so I don't have the big chunks of GET to describe the page links?

Also, anyone done anything neat with a pagination system that they want to share?

PHP:
//PAGINATION
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }
// one day per page
	$total_pages = count($daterange); 
	if ( $total_pages<$page ) { $page = $total_pages; }

	echo "<P><hr>";
	echo "Page: ";
	echo "<a href=reportlist.php?page=1&report=".$report_code.
	'&year_select_start='. $_GET['year_select_start'] .
	'&month_select_start='. $_GET['month_select_start'] .
	'&date_select_start='. $_GET['date_select_start'] .
	'&year_select_end='. $_GET['year_select_end'] .
	'&month_select_end='. $_GET['month_select_end'] .
	'&date_select_end='. $_GET['date_select_end'].
	">&lt;&lt;</a> ";
	$prev_page = $page-1;
	if ($prev_page == 0) { $prev_page=1; }
	echo "<a href=reportlist.php?page=".$prev_page."&report=".$report_code.
	'&year_select_start='. $_GET['year_select_start'] .
	'&month_select_start='. $_GET['month_select_start'] .
	'&date_select_start='. $_GET['date_select_start'] .
	'&year_select_end='. $_GET['year_select_end'] .
	'&month_select_end='. $_GET['month_select_end'] .
	'&date_select_end='. $_GET['date_select_end'].
	">&lt;</a> ";
	for ($i=1; $i<=$total_pages; $i++) { 
		if ($i == $page ) echo "<big><b>";
		if ( $i - 500 == $page || $i + 500 == $page ) {
			echo "<a href=reportlist.php?page=".$i."&report=".$report_code.
			'&year_select_start='. $_GET['year_select_start'] .
			'&month_select_start='. $_GET['month_select_start'] .
			'&date_select_start='. $_GET['date_select_start'] .
			'&year_select_end='. $_GET['year_select_end'] .
			'&month_select_end='. $_GET['month_select_end'] .
			'&date_select_end='. $_GET['date_select_end'].
			">&nbsp;".$i."&nbsp;</a> ";		
		} elseif ( $i - 100 == $page || $i + 100 == $page ) {
			echo "<a href=reportlist.php?page=".$i."&report=".$report_code.
			'&year_select_start='. $_GET['year_select_start'] .
			'&month_select_start='. $_GET['month_select_start'] .
			'&date_select_start='. $_GET['date_select_start'] .
			'&year_select_end='. $_GET['year_select_end'] .
			'&month_select_end='. $_GET['month_select_end'] .
			'&date_select_end='. $_GET['date_select_end'].
			">&nbsp;".$i."&nbsp;</a> ";		
		} elseif ( $i - 50 == $page || $i + 50 == $page ) {
			echo "<a href=reportlist.php?page=".$i."&report=".$report_code.
			'&year_select_start='. $_GET['year_select_start'] .
			'&month_select_start='. $_GET['month_select_start'] .
			'&date_select_start='. $_GET['date_select_start'] .
			'&year_select_end='. $_GET['year_select_end'] .
			'&month_select_end='. $_GET['month_select_end'] .
			'&date_select_end='. $_GET['date_select_end'].
			">&nbsp;".$i."&nbsp;</a> ";		
		} elseif ( $i - 25 == $page || $i + 25 == $page ) {
			echo "<a href=reportlist.php?page=".$i."&report=".$report_code.
			'&year_select_start='. $_GET['year_select_start'] .
			'&month_select_start='. $_GET['month_select_start'] .
			'&date_select_start='. $_GET['date_select_start'] .
			'&year_select_end='. $_GET['year_select_end'] .
			'&month_select_end='. $_GET['month_select_end'] .
			'&date_select_end='. $_GET['date_select_end'].
			">&nbsp;".$i."&nbsp;</a> ";		
		} elseif ( $page + 5 < $i || $page - 5 > $i ) {
	// do nothing
		} else {
			echo "<a href=reportlist.php?page=".$i."&report=".$report_code.
			'&year_select_start='. $_GET['year_select_start'] .
			'&month_select_start='. $_GET['month_select_start'] .
			'&date_select_start='. $_GET['date_select_start'] .
			'&year_select_end='. $_GET['year_select_end'] .
			'&month_select_end='. $_GET['month_select_end'] .
			'&date_select_end='. $_GET['date_select_end'].
			">".$i."</a> ";		
		}
	if ($i == $page ) echo "</b></big>";
	};
	$next_page = $page+1;
	if ($next_page > $total_pages) { $next_page=$total_pages; }
	echo "<a href=reportlist.php?page=".$next_page."&report=".$report_code.
	'&year_select_start='. $_GET['year_select_start'] .
	'&month_select_start='. $_GET['month_select_start'] .
	'&date_select_start='. $_GET['date_select_start'] .
	'&year_select_end='. $_GET['year_select_end'] .
	'&month_select_end='. $_GET['month_select_end'] .
	'&date_select_end='. $_GET['date_select_end'].
	">&gt;</a> ";
	echo "<a href=reportlist.php?page=".$total_pages."&report=".$report_code.
	'&year_select_start='. $_GET['year_select_start'] .
	'&month_select_start='. $_GET['month_select_start'] .
	'&date_select_start='. $_GET['date_select_start'] .
	'&year_select_end='. $_GET['year_select_end'] .
	'&month_select_end='. $_GET['month_select_end'] .
	'&date_select_end='. $_GET['date_select_end'].
	">&gt;&gt;</a>";
	echo "<br />Total of: ".$total_pages." pages.";

Produces a result like this:

Page: << < 1 401 451 476 496 497 498 499 500 501 502 503 504 505 506 526 551 601 1001 > >>
Total of: 1827 pages.

Cheers all
 
sweet. I like it. I tried it out and it's good, but I still need to add in my gets, for the page links, as my page needs the dates and things each time, but I have put that into a variable so it's Ok. not sure whether I like this approach or mine better, but i didn't add this bit it, I guess it seomthing specific to your thing ? :

PHP:
if($pageNum != 1) {
        $count = ($numrows - $numrows) + ($offset + 1);
    } else {
        $count = $numrows - $numrows + 1;
    }
and the following lines where $count is used....

The code I pasted there .. $count = ($numrows - $numrows) + ($offset + 1); ... isn't that the same as $count = $offset +1 ? why the $numrows - $numrows?


edit and the resulting pagination bar looks like this for the benefit of anyone:
[First Page] [Prev] Showing page 16 of 56 pages [Next] [Last Page]
 
Back
Top Bottom