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 6 of 6

Thread: Paging Problem

  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Paging Problem

    I'm trying to do the typical paging seen on this forum. I'm partially finished but I'm stuck at limiting the page numbers per page. By that I mean is if I have 200 records to be displayed at 5 pages per page, then it would be something like 1,2,3,4,5,6,7,8...40.

    I want to have it so it says: Pages (3): « 1 [2] 3 »

    But I would like to go around my code. I've read just about every post here on paging, and while yes they go over how to do this, for some reason, I can't apply it to my code. Maybe I can get some help. Here's my code (only the portion that does paging):

    PHP Code:
    # $SnippetCount["snippet_count"] is the RecordCount for the record set
    $number_of_pages ceil($SnippetCount["snippet_count"]/$perpage);            
    $ax 0;  // tempcounter used to display page numbers            
    $current_page ceil($from/$perpage)+1;    // determine current page the user is on            


    // Generating Page Numbers
    for($i=1$i<=$number_of_pages$i++) {                
        if (
    $i==$current_page) {
            echo 
    "<font size=\"6\"class=\"GeneralLargeText\"><strong>$i</strong></font>&nbsp;";
            
    $ax $ax $perpage;
            continue;
        } else {
            echo  
    "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$ax&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">$i</font></a>&nbsp;";                
            
    $ax $ax $perpage;
        }                                
    }

    // Modifying $prev and $next variables so they don't go out of bounds.
    if($prev < -1$prev=0;
    if(
    $next >= $SnippetCount["snippet_count"]) $next $from;

    // [ Previous | Next ]
    echo "[ ";
    if(
    $current_page==1)
        echo 
    "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$next&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Next</font></a>&nbsp;";
    elseif(
    $current_page>&& $current_page<$number_of_pages){
        echo 
    "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$prev&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Previous</font></a>&nbsp;";
        echo 
    "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$next&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Next</font></a>&nbsp;";
    }            
    elseif(
    $current_page==$number_of_pages)
        echo 
    "<a href=\"".$_SERVER['SCRIPT_NAME']."?from=$prev&perpage=$perpage&orderby=".translate_fieldwords($orderby)."&ordertype=$ordertype\"><font class=\"TextSpecialMessage2\">Previous</font></a>&nbsp;";
    echo 
    "]"
    So for this, if I had 116 records, and if I was on page one at 5 records per page, the above would display:

    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [ Next ]
    Any help is greatly appreciated. Thanks!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I just added a pagination feature to a site I am working on. I am too tired now to go through the code and pull out just the pagination portions of the script so I attached the whole file but you can peruse through the attached file if you like.
    Attached Files Attached Files
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've recently written what i would like to dub my 'ultimate page numbering' script. I probably isn't but it sounds better like that.

    I'm going to post a proper annotated version on the scripts section of my site once i get around to redoing that bit of my site, so for now here's what i'm using.

    $page should be assigned to the current page, while $total is the number of records in the resultset. $currpage should be the the value of $_SERVER['PHP_SELF'].

    This will output a list of links that has a button for prev and next page, will always have the first and last links as well as having $lnkoff number of links between the current link and the links at either end. Hope it works out ok for you, i've just quily stripped the bits u wont need after pasting it in here.

    PHP Code:
    $perPage 10;
    $lnkoff 4;

    $totalPages ceil($total $perPage);
    $Ppage $page 1;
    $Npage $page 1;
    $startPage = ($page 2) ? $page $lnkoff ;
    $startPage = ($startPage 2) ? $startPage ;
    $spareright $startPage - ($page $lnkoff);
    $limit $page+$lnkoff+$spareright;
    $uptoPage = ($totalPages $limit) ? ($limit) : ($totalPages-1) ;
    $spareleft $limit $uptoPage;
    $startPage -= $spareleft;
    $startPage = ($startPage 2) ? $startPage ;
    $uptoPage = ($uptoPage > ($totalPages-1)) ? ($totalPages-1) : $uptoPage ;
    if (
    $totalPages != 1) {
        
    $pageNav "<p class=\"pagelinks\">\n<a";
        if(
    $page != 1){
            
    $pageNav .= " href=\"{$currpage}?page=$Ppage\"";
        }
        
    $pageNav .= ">&lt;&lt;&lt;</a> | \n";
        
    $href = ($page == 1) ? '' " href=\"{$currpage}?page=1\"" ;
        
    $pageNav .= "<a$href>1</a> | \n";
        
    $pageNav .= ($startPage 2) ? " ... | \n" '' ;
        for (
    $i=$startPage;$i<=$uptoPage;$i++) {
            if(
    $i != $page) {
                
    $href " href=\"{$currpage}?page=$i\"";
            } else {
                
    $href ='';
            }
            
    $pageNav .= "<a$href>$i</a> | \n";
        }
        
    $pageNav .= ($totalPages > ($uptoPage+1)) ? " ... | \n" '' ;
        
    $href = ($page == $totalPages) ? '' " href=\"{$currpage}?page=$totalPages\"" ;
        
    $pageNav .= "<a$href>$totalPages</a> | \n";
        
    $pageNav .= "<a";
        if(
    $page $totalPages){
            
    $pageNav .= " href=\"{$currpage}?page=$Npage\"";
        }
        
    $pageNav .= ">&gt;&gt;&gt;</a>\n</p>";

    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry for burging into the topic.

    $startPage = ($page > 2) ? $page - $lnkoff : 2 ;
    I see such coding style but what does it mean actually?

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    its an alternate and less verbose type of if/else , so
    PHP Code:
    <?
    if($x==1){
      
    $var ;
    }else{
      
    $var ;
    }
    ?>
    is equivalent to
    <?
    $var 
    = ($x==1) ? ;
    ?>
    e.g.
    <?
    echo ( isset( $x ) ) ? '$x is set' '$x is not set' ;
    ?>
    its often used purely for its brevity
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    It's use is discouraged for its obvious unreadability.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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