Jump to content

Recommended Posts

Hi guys,

I am currently using this script for pagination

[code]

$limitpages = 5;
$page_limit = $page + $limitpages;
if ($page_limit > $total_pages ){$page_limit = $total_pages ;}
$page_start = $page;
$page_stop = $page_start + $limitpages;

if ($page_stop > $total_pages)
{
$page_stop = $page_stop -$total_pages ;

$page_start = $page_start -$page_stop;
}


// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$host = $_SERVER['PHP_SELF'];


    echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$prev\">&lt;</a>&nbsp;";
}

for($i = $page_start; ($i <= $total_pages & $i <=$page_limit); $i++)
{
if(($page) == $i){
echo "$i ";
} else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$i\">$i</a>&nbsp;";
}
}

// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$next\">&gt;</a>";
}

echo "</div>";


[/code]


which is from a php tutorial here and modified with help from siguy to limit the number of pages.

It works great, apart from 1 little thing which is that you can move forward fine by clicking on a page number, and the code will display a few links for pages behind and ahead of the current page.  However if you then click the first link, after the page loads, the only way to go back is to keep clicking the back (<) button.  I have tried to modify it myself but I kept getting infinite loops and browser crashes!

If you want to see what I mean in action, click here:
http://host.hostingseries44.net/~charles7/exhibition.php?category=8

(this page will actually have around 1000 items in it soon, not the 40 odd just now, so it is necessary to limit the pages)


thank you very much for your time.
Link to comment
https://forums.phpfreaks.com/topic/14965-pagination-with-many-pages/
Share on other sites

alright, that math seems terribly convoluted given how simple this appears to be.  assuming $page is the current page you're on, and $totalpages is the total number of pages returned for the query:

[code]<?php

// set the limit of pages to show after the current
$upper_limit = 5;

// set the limit of pages to show before the current
$lower_limit = 5;

// calculate where we're starting the loop, and where we're stopping
$start = (($page - $lower_limit) <= 1) ? 1 : ($page - $lower_limit);
$stop = (($page + $upper_limit) >= $totalpages) ? $totalpages : ($page + $upper_limit);

// finally, show the links

// show a back if needed
if ($start > 1)
{
  // show back button (i'm sure you can slot this in yourself)
}

// show the pages themselves
for ($i = $start; $i <= $stop; ++$i)
{
  // show the number link, if ($i == $page), then show the number but not the link
}

// show the next link if necessary
if ($stop < $totalpages)
{
  // show the next button
}

?>[/code]

should work, but to be fair i haven't tested it.  give it a whirl and let me know if it comes up with errors.  the code within the if()s and for() can just be slotted in from your current.
thanks mate but i'm afraid it didn't do anything, didn't actually display previous, numbers or next link

(I did slot in the code you told me to)



i have edited the original code i posted to this

[code]
$limitpages = 3;


if(($page - $limitpages)<=1)
{
$pagestart==1;
}
else
{
$pagestart==$page-$limitpages;
}

$page_stop = $page + $limitpages;

if ($page_stop > $total_pages)
{
$page_stop==$total_pages;
}


// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$host = $_SERVER['PHP_SELF'];


    echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$prev\">&lt;</a>&nbsp;";
}

for($i = $page_start; $i <= $page_stop; $i++)
{
if(($page) == $i){
echo "$i ";
} else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$i\">$i</a>&nbsp;";
}
}

// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$next\">&gt;</a>";
}

echo "</div>";

[/code]



and this works as it should, except that it includes minus number pages (eg when on page 1, page links are "-2 -1 0 1 2 3 4" where it should start at 1, and when on the last real page (page 9), the page links are "6 7 8 9 10 11 12", where it should just be 6-9

as before you can see what I mean in action here:

http://host.hostingseries44.net/~charles7/exhibition.php?category=8


any help much appreciated

cheers
ok I have changed the code at the top which sets the $page_start and $page_stop again, I don't have the minus numbers problem or the too many page links problem, however now, if the page I am on gets past 6 out of 9 (which is $totalpages - $limitpages), then the page number links won't display (ie on page 7, 8 and 9 only previous and next links show)

this is the updated code

[code]
$limitpages = 3;

if($page-$limitpages<1)
{
$page_start==1;
}
else
{
$page_start = $page-$limitpages;
}



if ($page+$limitpages>$total_pages)
{
$page_stop==$total_pages;
}
else
{
$page_stop = $page + $limitpages;
}


// Build Previous Link
if($page > 1){
$prev = ($page - 1);
$host = $_SERVER['PHP_SELF'];


    echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$prev\">&lt;</a>&nbsp;";
}

for($i = $page_start; $i <= $page_stop; $i++)
{
if(($page) == $i){
echo "$i ";
} else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$i\">$i</a>&nbsp;";
}
}

// Build Next Link
if($page < $total_pages){
$next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?category=$category&amp;page=$next\">&gt;</a>";
}

echo "</div>";



[/code]
If you're getting from a database, you could make use of LIMIT in your SQL. It allows you to set offset and return parameters (e.g. LIMIT 10, 10 would start 10 records in and get the 10 records)

You could have a two constant somewhere in your PHP file that sets how much you want on a page.

For example $offset could be how many pages in and $returned could be the pages returned.

Then, use the constants on the LIMIT.

You could also use these constants as a way of calculating how many pages you have by dividing your total records (your total pages) by the constant. A result of 10.3, for example, would indicate 9 pages of 10 records and 1 page of 3 records.

If you want to change your pagination at any time, you simply change the constants and it is changed throughout your entire code!
haha after trying to sort the logic of this for so long I missed the most obvious mistake!

I found the problem in this line:

if ($page+$limitpages>$total_pages)
{
$page_stop==$total_pages;
}


i used the comparison operator ==, instead of assignment statement =

silly me...!

all working now



thank you all very much for your help, and offers of help


cheers
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.