Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Paginating

  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  • #2
    LAMPE
    Guest
    you would just need to count the pages, and make multiple for loops.

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

    }

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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:
    Assuming 20 pages, and currently on page 10:
    &#171; Previous ... 6 7 8 9 10 11 12 13 14 ... Next &#187;
    Last edited by Inigoesdr; 05-18-2008 at 07:51 PM.

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thx for the help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •