Results 1 to 4 of 4

Thread: split pages?

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default split pages?

    Hello,

    I need to split my pages up if the row result is higher then x numbers but I have no idea on how to do this.. Is there any tutorial on how to do something like this? And also, is it possible to echo how many rows the browser found?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    So, you basically want a pagination script? There are several floating around the internet as well as some here on DD. Take a look at the following script to see if that suits your needs:

    Code:
    <?php
         include('dbconnect.php');
               
    // If page is set
    if($_GET['page'] && is_numeric($_GET['page'])) {
        $page = $_GET['page'];
    }
    
    // Set default page
    else {
        $page = 1;
    }
    
    // Max results per page
    $max = 7;
    
    // Current page
    $cur = (($page * $max) - $max);
    
    // Get data
    $qry = mysql_query("SELECT * FROM `test`");
    $total = mysql_num_rows($qry); // count results
    
    // Get amount of pages
    $total_pages = ($total / $max);
        
    // Is page # more than one
    if($page > 1) {
        $prev = ($page - 1);
        $prevLink = '<a href="?p='.$p.'&page='.$prev.'">&lt;&lt;Previous</a>';
    }
    
    // For each page less than total pages
    for($i=1;$i<$total_pages+1;$i++) {
        // If page = current page
        if($page == $i) {
            $nav .= ' <b>'.$i.'</b> ';
        } else {
            // Echo link to previous page
            $nav .= ' <a href="?p='.$p.'&page='.$i.'">'.$i.'</a> ';
        }
    }
    
    // If page is less than total pages, show next link
    if($page < $total_pages) {
        $next = ($page + 1);
        $nextLink = '<a href="?p='.$p.'&page='.$next.'">Next&gt;&gt;</a>';
    }
    
    ########## End Pagination Script ##########
    
    $info = mysql_query("SELECT * FROM `test` LIMIT $cur, $max");
    
        while($row = mysql_fetch_array($info)) {
            echo $row['val'].'
            <br />';
        }
    
    if ($total_pages > 1) {
    echo 'Pages: '.$prevLink . $nav . $nextLink;
    }
    
    ?>
    If not, then you may want to look at http://www.php-mysql-tutorial.com/php-mysql-paging.php and view their tutorial.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That code is perfect, big thanks for pointing it out

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    No problem, let us know if you need any more help.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

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