Jump to content

Paginating guestbook entries


graham23s

Recommended Posts

Hey GUys,

 

been at this for a bit now i thought this would be fairly easy but i must have overlooked something, i have 3 entries in mysql (im testing with 1 entry per page) the links show as:

 

<<< 1 2 3 >>>

 

(which is half right) but all 3 entries are on the 1 page instead of 1 entry per all 3 pages, can nayone see where i have went wrong:

 

        ## Pagination start #############################################################
	// 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` ORDER BY `date` DESC $from, $max_results"); 

        ## Pagination start #############################################################
      echo '<br />
            <table width="500" border="1" bordercolor="#000000" cellpadding="5" cellspacing="0" />
            <tr>
            <td colspan="2" bgcolor="#004E98"><img src="images/guestbook_entries.gif" /></td>
            </tr>';
            
            ## 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 "<tr><td 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\"><<< </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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.