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
 
Depends on how your database is setup but I use the below code for listing pages of data.

PHP:
<?php
    include 'scripts/system/config.php';
    include 'scripts/system/opendb.php';

    // how many rows to show per page
    $rowsPerPage = 8;

    // by default we show first page
    $pageNum = 1;

    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
        $pageNum = $_GET['page'];
    }

    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;

    // how many rows we have in database
    $query   = "SELECT * FROM newsfeed";
    $result  = mysql_query($query) or die('Error, query failed');
    $numrows = mysql_num_rows($result);
                    
    if($pageNum != 1) {
        $count = ($numrows - $numrows) + ($offset + 1);
    } else {
        $count = $numrows - $numrows + 1;
    }
                    
    $query  = "SELECT id,title,description,pubdate,url FROM newsfeed ORDER BY id DESC LIMIT $offset, $rowsPerPage";
    $result = mysql_query($query) or die('Error, query failed');

    // print the random numbers
    echo "<div class=\"newsar\"><dl>";                    
        while(list($id,$title,$description,$pubdate,$url) = mysql_fetch_array($result))
        {
            echo "<dt><strong>$count: </strong><a rel=\"external\" href=\"$url\">$title</a></dt>
            <dd><i>$pubdate</i> - $description</dd><br/>";
            $count++;
        }

    echo "</dl></div>";

    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);

    $self = $_SERVER['PHP_SELF'];

    // creating 'previous' and 'next' link
    // plus 'first page' and 'last page' link

    // print 'previous' link only if we're not
    // on page one
    echo "<p>";
                    
    if ($pageNum > 1)
    {
        $page = $pageNum - 1;
        $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
                        
        $first = " <a href=\"$self?page=1\">[First Page]</a> ";
    } else {
        $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
        $first = ' [First Page] '; // nor 'first page' link
    }

    // print 'next' link only if we're not
    // on the last page
    if ($pageNum < $maxPage)
    {
        $page = $pageNum + 1;
        $next = " <a href=\"$self?page=$page\">[Next]</a> ";
                        
        $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    } else {
        $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
        $last = ' [Last Page] '; // nor 'last page' link
    }

    // print the page navigation link
    echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
    echo "</p>";

    include 'scripts/system/closedb.php';
?>
 
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