Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-18-2008, 02:00 PM   PM User | #1
mokakid
New Coder

 
Join Date: Apr 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
mokakid is an unknown quantity at this point
Paginating

I don't have a clue about how to make a pagination system that it's 100% practical

i want to have something like this... in my example page 10 is the page where i am.

Go to page: First ... 6 7 8 9 10 11 12 13 14 ... Last

What i want? I want "First" 4 pages "the selected page" another 4 pages "Last"
and i want the code to automatically correct if the selected page is 1 so it will look something like this:

1 2 3 4 5 6 7 8 ... Last
or
First ... 5 6 7 8 9 10 11 12 13

i hope you understand what i want
mokakid is offline   Reply With Quote
Old 05-18-2008, 06:26 PM   PM User | #2
LAMPE
Guest

 
Posts: n/a
you would just need to count the pages, and make multiple for loops.

for($a = 1; $a <= $num_of_pages; $a++){

}
  Reply With Quote
Old 05-18-2008, 07:12 PM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by LAMPE View Post
make multiple for loops.
Why would you need multiple loops?

mokakid, here is an example function you can use. Modify it however you need.
PHP Code:
function paginate($current_page$total_pages$link)
{
    
$prev $pages $next '';
    
    if(
$current_page == 0)
        
$current_page 1;    
    elseif(
$current_page 1)
        
$prev sprintf('<a href="%s&page=%d">&laquo; Previous</a> '$link$current_page 1);
    
    for (
$i 1$i <= $total_pages$i++)
    {
        if(
$i == && $current_page 5)
        {
            
$i $current_page 4;
            
$pages .= ' ... ';
        }
        
        if(
$current_page == $i)
            
$pages .= $i ' ';
        else
            
$pages .= sprintf('<a href="%s&page=%d">%d</a> '$link$i$i);

        if(
$i == $current_page && $total_pages $current_page 4)
        {
            
$pages .= ' ... ';
            break;
        }
    }
    
    if(
$current_page $total_pages)
        
$next sprintf('<a href="%s&page=%d">Next &raquo;</a>'$link$current_page 1);

    return 
$prev $pages $next;
}

echo 
'Assuming 20 pages, and currently on page 10:<br />' paginate(1020'/search.php?q=stuff'); 
Outputs:
Quote:
Assuming 20 pages, and currently on page 10:
« Previous ... 6 7 8 9 10 11 12 13 14 ... Next »

Last edited by Inigoesdr; 05-18-2008 at 07:51 PM..
Inigoesdr is offline   Reply With Quote
Old 05-19-2008, 04:51 PM   PM User | #4
mokakid
New Coder

 
Join Date: Apr 2008
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
mokakid is an unknown quantity at this point
thx for the help
mokakid is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:01 AM.


Advertisement
Log in to turn off these ads.