ted_chou12 Posted January 3, 2007 Share Posted January 3, 2007 [code] <?php $diary = $username.'diary'; // connect to mysql below require("../mysqlconnection2.php"); // Set how many rows to display on each page $limit = 10; // Query the database to get the total number of rows $query_count = "SELECT * FROM $diary"; $result_count = mysql_query($query_count) or die (mysql_error()); $totalrows = mysql_num_rows($result_count); if(isset($_GET['page'])){ // Checks if the $page variable is empty (not set) $page = $_GET['page']; // If it is empty, we're on page 1 } else { $page = 1; } // Set the start value $startvalue = $page * $limit - ($limit); // Query the database and set the start row and limit $sql = "SELECT * FROM $diary LIMIT $startvalue, $limit"; $res = mysql_query($sql) or die (mysql_error()); // Do a quick check to see if there are any records to display if(mysql_num_rows($res) == 0){ echo "<p>There are no entries the blog right now!</p>"; } // Start loop through records while ($r = mysql_fetch_array($res)){ $entrydate = strtotime($r['entrydate']) + $row['timeoffset']; $entrydate = date('M d, Y h:i A',$entrydate); // Echo out the records with wordwrap, remove if you like echo "<p><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr> <td align=center width=\"10%\" height=\"50%\" bgcolor=\"#CCCCCC\"> <img src=\"images/". $r['entryicon'] .".gif\"></td> <td align=left width=\"90%\" bgcolor=\"#CCCCCC\"><b>". $r['entrytitle'] ."</b></td> <td align=center width=\"100%\" bgcolor=\"#CCCCCC\"><a href=\"blogedit.php?id=". $r['id'] ."\">Edit</a></td></tr></table><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"> <td align=left width=\"100%\" bgcolor=\"#f2f2f2\">Posted on: ". $entrydate ."</td></table><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"> <tr><td align=left width=100% bgcolor=\"#CCCCCC\"><p>". $r['entrycontent'] ."</p></td></tr></table></p><br />";} // Start links for pages echo "<p align=center>"; // Sets link for previous 25 and return to page 1 if($page != 1){ $pageprev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\"><<</a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV </a> "; }else{ echo "PREV "; } // Find out the total number of pages depending on the limit set $numofpages = $totalrows / $limit;//##############################here, the total num rows is divided by the limit. $totalpages = round($numofpages); // Loop thru all the pages and echo out the links for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo "[".$i."] "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } // Check for straglers after the limit blocks if(($totalrows % $limit) != 0){ if($i == $page){ echo "[".$i."] "; }else{ echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } // Print out the Next 25 and Goto Last page links if(($totalrows - ($limit * $page)) > 0){ $pagenext = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT </a> "; echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a> "; }else{ echo("NEXT"); } echo "</p>"; // Free results mysql_free_result($res); // Close mysql connection mysql_close($mysql_conn); ?> [/code]i had 12 results in my database, and it appears this:PREV [1] 2 NEXT >> it looks right but this ">>" doesnt link right, it links to the first page, so I was thinking. see the orange text:$numofpages = $totalrows / $limit; <<<here is not right at all, say if we have 12 results, the totalrows will be 12 limit is 10 after the divions, it would be 1.2 and round... that doesnt go to 2 at all. How does that work????ThanksTed Quote Link to comment Share on other sites More sharing options...
sasa Posted January 3, 2007 Share Posted January 3, 2007 use ceil() function [code]$totalpages = ceil($numofpages);[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 3, 2007 Author Share Posted January 3, 2007 thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.