twsowerby Posted December 16, 2007 Share Posted December 16, 2007 Hi guys you've probably heard this a hundred times but I have been following the pagination tutorial by TheReverend at http://www.phpfreaks.com/tutorials/43/5.php?topic_id=6691&hl=6691#6691 and it doesnt quite seem to work properly. I have tried to access the discussion links as it looked like people have had the same problem as me, but they arent working. I will post my code below showing how I have implemented the code: <?php // File: message.php define('IN_FORUM', true); //include_once "sqlconnect.php"; include_once "includes/functions.php"; include "includes/header.php"; $genreid=$_GET['genreid']; $id=$_GET['id']; $title=$_GET['title']; echo "<link rel='stylesheet' href='style.css' type='text/css'>"; echo "<a href='index.php'>Home</a>"; if(isset($_SESSION['session_loggedIn']) && $_SESSION['session_loggedIn'] == 1) { echo " - <a href='reply.php?id=$id&genreid=$genreid&title=$title'>Reply</a><br />"; } $limit = 5; $query_count = "select count(*) from forum_posts where postid='$id' or parentid='$id'"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ // Checks if the $page variable is empty (not set) $page = 1; // If it is empty, we're on page 1 } $limitvalue = $page * $limit - ($limit); // Ex: (2 * 25) - 25 = 25 <- data starts at 25 $getposts = "select * from forum_posts where postid='$id' or parentid='$id' order by realtime asc limit $limitvalue, $limit"; $getposts2=mysql_query($getposts) or die("Error: " . mysql_error()); if(mysql_num_rows($getposts2) == 0){ echo("Nothing to Display!"); } $bgcolor = "#E0E0E0"; echo "<table class='maintable'>"; echo "<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>"; while($row=mysql_fetch_array($getposts2)) { if($bgcolor == "#E0E0E0"){ $bgcolor = "#B0BFEF"; }else{ $bgcolor = "#E0E0E0"; } echo "<tr bgcolor=".$bgcolor." class='mainrow'><td valign='top'>$row[author]</td><td valign='top'>Posted on $row[showtime]<br><hr>"; $message=strip_tags($row['post']); $message=bb($message); $message=nl2br($message); echo "$message<hr>"; if(isset($_SESSION['session_loggedIn']) && $_SESSION['session_loggedIn'] == 1 && $_SESSION['session_userName'] == $row[author]) { echo "<a href='edit.php?id=$row[postid]&title=$row[title]'>Edit Post</a>"; } echo "</td></tr>"; } echo "</table>"; if($page != 1){ $pageprev = $page--; // Fancy way of subtracting 1 from $page echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV" .$limit."</a> "); /* Tip: It is a good idea NOT to use $PHP_SELF in this link. It may work, but to be 99.9% sure that it will, be sure to use the actual name of the file this script will be running on. Also, the adds a space to the end of PREV, and gives some room between the numbers. */ }else echo("PREV".$limit." "); // If we're on page 1, PREV is not a link $numofpages = $totalrows / $limit; /* We divide our total amount of rows (for example 102) by the limit (25). This will yield 4.08, which we can round down to 4. In the next few lines, we'll create 4 pages, and then check to see if we have extra rows remaining for a 5th page. */ for($i = 1; $i <= $numofpages; $i++){ /* This for loop will add 1 to $i at the end of each pass until $i is greater than $numofpages (4.08). */ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF&page=$i\">$i</a> "); } /* This if statement will not make the current page number available in link form. It will, however, make all other pages available in link form. */ } // This ends the for loop if(($totalrows % $limit) != 0){ /* The above statement is the key to knowing if there are remainders, and it's all because of the %. In PHP, C++, and other languages, the % is known as a Modulus. It returns the remainder after dividing two numbers. If there is no remainder, it returns zero. In our example, it will return 0.8 */ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF&page=$i\">$i</a> "); } /* This is the exact statement that turns pages into link form that is used above */ } // Ends the if statement if(($totalrows - ($limit * $page)) > 0){ /* This statement checks to see if there are more rows remaining, meaning there are pages in front of the current one. */ $pagenext = $page++; // Fancy way of adding 1 to page echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); /* Since there are pages remaining, this outputs NEXT in link form. */ }else{ echo("NEXT".$limit); /* If we're on the last page possible, NEXT will NOT be displayed in link form. */ } include "includes/footer.php"; ?> At the moment the limit works fine but the PREV and NEXT links dont work and it doesnt display the page numbers either. Any help would be greatly appreciated, Cheers! Tom Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted December 16, 2007 Share Posted December 16, 2007 Look at my reply to this post http://www.phpfreaks.com/forums/index.php/topic,172220.msg763400.html#msg763400 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 16, 2007 Share Posted December 16, 2007 Here is another working example you can look at <?php $limit = 10; $query_count = "SELECT count(*) FROM table"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0, 0); $numofpages = ceil($totalrows/$limit); if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1; $offset = ($page - 1) * $limit; $query = "SELECT * FROM table LIMIT $offset, $limit"; echo $query .'<br>'; $result = mysql_query($query) or die("Error: " . mysql_error()); //Exit if no records to display if (mysql_num_rows($result) == 0) { exit("Nothing to Display!"); } while ($row = mysql_fetch_array($result)) { echo $row['col']."</br>"; } //Enable the Prev link if not first page if ($page > 1) { echo("<a href=\"index.php?page=".($page-1)."\">PREV</a> "); } else { echo("PREV "); } //Create links for each page in report for ($i=1; $i<=$numofpages; $i++) { if ($i == $page) { echo "$i "; } else { echo "<a href=\"index.php?page=$i\">$i</a> "; } } //Enable the Next link if not last page if ($page < $numofpages) { echo("<a href=\"index.php?page=".($page+1)."\">NEXT</a>"); } else { echo("NEXT"); } ?> Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted December 16, 2007 Share Posted December 16, 2007 can someone from the phpfreaks team please fix the tutorial so that this problem does not come up again? Quote Link to comment Share on other sites More sharing options...
twsowerby Posted December 16, 2007 Author Share Posted December 16, 2007 Thanks for the help guys, got it working now. And yes would be good if someone could fix the tutorial! Tom *SOLVED* 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.