SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2008
    Location
    New Jersey, USA
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trouble with Pagination

    Hey guys,

    I've just set up my first pagination code, Because I'm trying to learn PHP, I set this up myself (it's my own code, so it may be semantically stupid and/or repetitive).

    Anyway, I'm trying to pull rows out of a database and list them on a page, 10 at a time. Everything works fine, but on page 2 I get 20 results as opposed to 10. I can't figure out why.

    PHP Code:
    <?php
        $page 
    $_GET['page'];
        if(!isset(
    $page))
            
    $page 1;
        
    $results_per_page 10;
        
    $get_records mysql_query("SELECT `message` FROM `messages`");
        
    $total_records mysql_num_rows($get_records);
        
    $total_pages ceil($total_records $results_per_page);
        
    $lower_limit = (($page-1) * 10);
        
    $upper_limit $lower_limit 10;
        
        
    $sql "SELECT `message` FROM `messages` ";
        
    $sql .= "LIMIT $lower_limit$upper_limit";
        
    $result mysql_query($sql);
        
      
    mysql_close($con);
    ?>

    .............
    html stuff
    .............

    <?php
                
    if ($page 1) {
                    
    $url = ("/chat.php?page=" . ($page 1));
                    echo 
    "<a href=\"$url\">&laquo;</a>\n";
                }
                
                for (
    $i 1$i $total_pages$i++) {
                    
    $url "/chat.php?page=" $i;
                    echo 
    "<a href=\"$url\">$i</a>\n";
                }
                
                if (
    $page $total_pages) {
                    
    $url = ("/chat.php?page=" . ($page 1));
                    echo 
    "<a href=\"$url\">&raquo;</a>\n";
                }
        
          while((
    $row mysql_fetch_array($result)) !== FALSE){
        
    ?>
        
          <div><?php echo $row['message']; ?></div>
        
        <?php
        
    }
        
    ?>
    Can anyone see what's going wrong here?

    Thanks a lot,
    Dan

  2. #2
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out the MySQL manual: http://dev.mysql.com/doc/refman/4.1/en/select.html

    You are using the LIMIT clause incorrectly. The second parameter shouldn't be $upper_limit, it should be $results_per_page.

    And just a code hint: You aren't using $results_per_page when defining your upper and lower limits, but you should be.
    MySQL v5.1.58
    PHP v5.3.6

  3. #3
    SitePoint Addict
    Join Date
    Feb 2008
    Location
    New Jersey, USA
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh - awesome. You're right. I was using LIMIT incorrectly. It works perfectly now (with a few extra minor tweaks).

    Thanks BrandonK!


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
  •