Implementing a multi page PHP system for SQL Query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Trinitrotoluene
Forum Newbie
Posts: 2
Joined: Tue Aug 25, 2009 6:52 pm

Implementing a multi page PHP system for SQL Query

Post by Trinitrotoluene »

Hello all,

I am currently trying trying to re-write a piece of PHP code for multi page navigation. The code works very well but unfortunately it is no pulling over a thousand rows from the database and is subsequently returning over 100 pages, like this:

Image

What I am looking to do is to make some sort of a limit so it will only display say pages 1 to 5 then the equivalent of a "more" IE: 1,2,3,4,5 ...

This is the code as it is at the moment; I have no idea where to start so any help anyone could provide would be very much appreciated.

The "back end" code:

Code: Select all

// This is the logic for the page navigation
$pages = intval($numrows/$limit); // Number of results pages.
 
// $pages now contains int of pages, unless there is a remainder from division.
 
if ($numrows%$limit) {
    $pages++;} // has remainder so add one page
    $current = ($page/$limit) + 1; // Current page number.
 
if (($pages < 1) || ($pages == 0)) {
    $total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
 
else {
    $total = $pages;} // Else total pages is $pages value.
    $first = $page + 1; // The first result.
 
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
    $last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
    $last = $numrows;} // If last results page, last result equals total number of results.
The "Display" code

Code: Select all

// Display the amount of pages code
if ($page != 0) { // Don't show back link if current page is first page.
    $back_page = $page - $limit;
    echo("<a href=\"http://www.supernaturalufo.com/index.php?c=$categoryid&page=$back_page&limit=$limit\">back</a>    \n");}
 
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
    $ppage = $limit*($i - 1);
    if ($ppage == $page){
    echo("<b>$i</b> \n");} // If current page don't give link, just text.
    else{
    echo("<a href=\"http://www.supernaturalufo.com/index.php?c=$categoryid&page=$ppage&limit=$limit\">$i</a> \n");}
}
 
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
    $next_page = $page + $limit;
    echo("    <a href=\"http://www.supernaturalufo.com/index.php?c=$categoryid&page=$next_page&limit=$limit\">next</a>\n");}
?>
            <font color="#2c3644">|      <?=$numrows?>
If you want to see the site as rendered PHP it is http://www.supernaturalufo.com

Thanks in advance,

Trinitrotoluene
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Implementing a multi page PHP system for SQL Query

Post by Christopher »

Search these forums for "pagination"
(#10850)
Post Reply