85 users online (0 members and 85 guests)  

Thread: paging in PHP


  Results 1 to 2 of 2

Related

  1. paging in perl    Forum: CGI Perl Forum
    Replies: 1
  1. #1
    r4874C's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2006
    Posts
    7

    paging in PHP

    I have a question about paging in PHP. could anybody give information or links to articles on how to divide web page into several pages in perl. in my guestbook entries are stored in a text file.I want to create links like this prev 1 2 3 4 5 next

  2. #2
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: paging in PHP

    As the length string in PHP is theoretically unlimited:

    In a messy mixture of code & English!
    PHP Code:
    Set $pages 1
    Read the original page into $original
    While (strlen($original) > $maxpagelength) {
        
    $subpage[$pages] = substr($original,0,$maxpagelength)
        
    // break page on space or <return>
        
    while substr($subpage[pages],-1) != " " and substr($subpage[pages] != "\n") {
            
    reduce length of $subpage[$pagesby 1
        
    }
        
    $original substr($originalstrlen($subpage[$pages]);
        
    $pages++;

    Then you have the page split into pages, held in an array, and you can create the menu with (given that $page is the current page)
    PHP Code:
    if ($page 1) { echo "<a href='link to $pages[$page-1]'>"; }
    echo 
    "prev"// only create link if on page 2 or higher
    if ($page 1) { echo "</a>"; }
    for (
    $menu 1$menu <= $pages$menu++) {
        if (
    $menu != $page) { echo "<a href='link to $pages[$menu]'>"; }
        echo 
    " | $menu"// don't create link to self
        
    if ($menu != $page) { echo "</a>"; }
    }
    if (
    $page $pages) { echo "<a href='link to $pages[$page+1]'>"; }
    echo 
    "next"// only create link if there is a next page
    if ($page $pages) { echo "</a>"; } 
    That needs a lot of tidying up, but I think the principal should work.
    I'm not near my development machine right now, so I can't test it ...



Tags for this Thread