SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Paginating guestbook entries

    Hi Guys,

    i thoguht this part was gonna be a breeze but something isn't right, i have 4 entries in a test database i put the max results per page to 1 as a test for th epagination the links are correct they show:

    <<< 1 2 3 4 >>>

    BUT they show all 4 per page rather than just 1

    can anyone see what i have done wrong:

    PHP Code:
          ## echo table #####################################################################
          
    echo '<br />
                <table width="500" border="1" bordercolor="#000000" cellpadding="3" cellspacing="0" />
                <form action="'
    .$_SERVER['PHP_SELF'].'" method="POST" />
                <tr>
                <td bgcolor="#004E98" colspan="2"><img src="images/guestbook.gif" /></td>
                </tr>
                <tr>
                <td align="right">Please Enter Your Name:</td><td align="left"><input type="text" name="name" size="30" /></td>
                </tr>
                <tr>
                <td align="right">Please Enter Your E-Mail:</td><td align="left"><input type="text" name="email" size="30" /></td>
                </tr> 
                <tr>
                <td align="right">Please Enter Location:</td><td align="left"><input type="text" name="from" size="30" /></td>
                </tr> 
                <tr>
                <td align="right">Your Message To Eileen:</td><td align="left"><textarea name="message" cols="30" rows="10"></textarea></td>
                </tr>  
                <tr>
                <td bgcolor="#004E98" colspan="2" align="right" /><input class="btn" type="submit" name="submit_entry" value="Add Guestbook Entry >>>" onclick="return confirm(\'Are You Sure All Fields Are Filled In?\');"></td>
                </tr>      
                </table></form>'
    ;
                
             
    ## loop out the entries ##################################################### 
             ## Pagination start #############################################################
            
    echo "<center>";
            
    // If current page number, use it 
            // if not, set one! 
            
            
    if(!isset($_GET['page'])){ 
                
    $page 1
            } else { 
                
    $page $_GET['page']; 
            } 
            
            
    // Define the number of results per page 
            
    $max_results 1;         
            
            
    // Figure out the limit for the query based 
            // on the current page number. 
            
    $from = (($page $max_results) - $max_results);  
            
            
    // Perform MySQL query on only the current page number's results 
            
            
    $sql mysql_query("SELECT * FROM `guestbook` LIMIT $from$max_results"); 
            
            
    ## Pagination start #############################################################       
                
                ## query results then loop ##################################################
                
    $query_gb "SELECT * FROM `guestbook` ORDER BY `date` DESC";
                
    $result_gb mysql_query($query_gb) or die (mysql_error());
                
                
    ## the loop #################################################################
                
    while($row mysql_fetch_array($result_gb)) {
                
                
    $id $row['id'];
                
    $name $row['name'];
                
    $email $row['email'];
                
    $from $row['from'];
                
    $message $row['message'];
                
                echo 
    "<table width=\"500\" border=\"1\" bordercolor=\"#000000\" cellpadding=\"5\" cellspacing=\"0\" />
                      <tr>
                      <td bgcolor=\"#5CB3FF\" colspan=\"2\" align=\"left\"><b>Entry Number:</b> 
    $id</td></tr><tr><td colspan=\"2\" align=\"left\"><b>Name:</b> <font color=\"red\"><b>$name</b></font></td></tr><tr><td colspan=\"2\" align=\"left\"><b>From:</b> $from</td><tr><td colspan=\"2\" align=\"left\"><b>E-Mail Address:</b> $email</td></tr><tr><td align=\"left\" colspan=\"2\" bgcolor=\"#ffffff\">$message</td></tr>";
                echo 
    '</table><br />';
                
                }
                
            
    ## Pagination end ###############################################################
            
            // Figure out the total number of results in DB: 
            
    $total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM `guestbook`"),0); 
            
            
    // Figure out the total number of pages. Always round up using ceil() 
            
    $total_pages ceil($total_results $max_results); 
            
            
    // Build Previous Link 
            
    if($page 1){ 
                
    $prev = ($page 1); 
                echo 
    " <a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><font color=\"#000000\"><<<&nbsp;</font></a> "
            } 
            
            for(
    $i 1$i <= $total_pages$i++){ 
                if((
    $page) == $i){ 
                    echo 
    "[<b>$i</b>] "
                    } else { 
                        echo 
    "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\"><font color=\"#000000\">$i</font></a> "
                } 
            } 
            
            
    // Build Next Link 
            
    if($page $total_pages){ 
                
    $next = ($page 1); 
                echo 
    "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"><font color=\"#000000\"> >>></font></a>"
            } 
            echo 
    "<br /><br />"
            
            
    ## Pagination end ###############################################################
                 
          

    cheers

    Graham

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
            $sql mysql_query("SELECT * FROM `guestbook` LIMIT $from$max_results"); 

            
    ## Pagination start #############################################################                   
                ## query results then loop ##################################################

                
    $query_gb "SELECT * FROM `guestbook` ORDER BY `date` DESC";

                
    $result_gb mysql_query($query_gb) or die (mysql_error()); 
    You have created two queries and modified only the one that's not being used with the proper LIMIT

    I would also maintain the ORDER BY in your new query, otherwise who knows what posts are going to be on what page.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2006
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah lol i see now fixed it thanks a lot Dan

    Graham


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
  •