marcus Posted April 28, 2007 Share Posted April 28, 2007 I am creating pagination for forums, to limit 20 rows per page. I have the limiting correct, but when it comes to echoing the "Previous PAGENUMBERS Next" part it shows: Previous 1 Next It shows this even when there should be 5 pages. Also, the Previous 1 and Next are not linked, Previous and 1 are only linked when page > 1. $limit = 20; $query_count = "SELECT count(*) FROM `replies` WHERE `topic` =$id"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); $pie = $_GET ; intval($pie); if(!$pie){ $pie=1; } if($pie != 1){ $pageprev = $pie-1; echo "<a href=\"?act=topic&id=$id&page=$pageprev\">Previous </a> "; }else{ echo "Previous "; } $limit = 20; $query_count = "SELECT count(*) FROM `replies` WHERE `topic` =$id"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); $pageamt = $totalrows/$limit; for($pid = 1; $pid <= $pageamt; $pid++){ if($i == $pie){ echo "$pid "; }else{ echo "<a href=\"?act=topic&id=$id&page=$pid\">$pid</a> "; } } $giv = $totalrows%$limit; if($giv != 0){ if($pid == $pie){ echo "$pid "; }else{ echo "<a href=\"?act=topic&id=$id&page=$pid\">$pid</a> "; } } $give = $totalrows-($limit*$pie); if($give > 0){ $pagenext = $pie+1; echo "<a href=\"?act=topic&id=$id&page=$pagenext\">Next</a>"; }else{ echo "Next"; } Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/ Share on other sites More sharing options...
bobleny Posted April 28, 2007 Share Posted April 28, 2007 I think you have a few errors... Why are you repeating this twice? $limit = 20; $query_count = "SELECT count(*) FROM `replies` WHERE `topic` =$id"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); You would want to use the first one to count the rows, yes that is fine. The second time though, you need to fetch the rows... Probably the biggest error, is that your not actually limiting your results... That is why it is showing you only the one. I recommend that you follow this guide. It is oddly similar to your code. Here is the full pagination code: http://www.phpfreaks.com/tutorials/43/5.php Here is the start of the guide (skipping the intro): http://www.phpfreaks.com/tutorials/43/1.php If you still need help, I will certainly help you! I just think that the guide is probably the best way to help you at this point. Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-240518 Share on other sites More sharing options...
acoustika Posted September 27, 2007 Share Posted September 27, 2007 he... ive just used that tutorial and its not working very good... on my NEXT it counts down 1 and its only one page 1 the page lik actual counts right... then u see alot og response to the tutorial... but on this great php site, which was supposed to be proff... u cant see the responses... i must laugh LOL Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356684 Share on other sites More sharing options...
sasa Posted September 27, 2007 Share Posted September 27, 2007 change line $totalrows = mysql_num_rows($result_count); to $totalrows = mysql_result($result_count, 0, 0); and line if($i == $pie){ to if($pid == $pie){ Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356698 Share on other sites More sharing options...
acoustika Posted September 27, 2007 Share Posted September 27, 2007 well i made it work another way.... just changing $page++ and $page-- to $page+1 and $page-1 also makes it work sometimes "fancy" just doesnt do it :-) Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356729 Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 $page++ and $page-- where was that??? hopefully you weren't assigning those to a var instead of $page + 1 Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356734 Share on other sites More sharing options...
acoustika Posted September 27, 2007 Share Posted September 27, 2007 my script looks like this now and it works.... <?php $limit = 20; db_connect(); $result_count = mysql_query("SELECT * FROM galleries"); $totalrows = mysql_num_rows($result_count); echo $totalrows; if (empty($page)) { $page = 1; } $limitValue = $page*$limit - ($limit); $sql = mysql_query("SELECT * FROM galleries LIMIT $limitValue,$limit"); $div=0; while ($gal = mysql_fetch_array($sql)) { if ($div == 0) { echo '<div class="outer_gal">'; } echo '<div class="one_gal"><a href="'.$gal['link'].'" target="_blank"><img src="'.$gal['thumb'].'" /></a></div>'; $div++; if ($div == 5) { echo '<div class="clr"></div></div>';$div=0; } } if ($div < 5) { echo '<div class="clr"></div></div>'; } echo '<div>'; if($page != 1){ $pageprev = $page-1; echo("<a href=\"index.php?action=gallery&page=$pageprev\">PREV".$limit."</a> "); }else{ echo("PREV".$limit." "); } $numofpages = $totalrows / $limit; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"index.php?action=gallery&page=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"index.php?action=gallery&page=$i\">$i</a> "); } } if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page+1; echo("<a href=\"index.php?action=gallery&page=$pagenext\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); } echo '</div>'; mysql_free_result($result); include_once('footer.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356739 Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 very good. $pageprev = $page-1; is NOT the equivalent of $pageprev = $page--; Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356749 Share on other sites More sharing options...
acoustika Posted September 27, 2007 Share Posted September 27, 2007 Nope ;-) not in this case Well i knew it was something about how it counted it, or how the math was done... first i was trying some like ++$page and --$page but that only fixed the -page thing and then it suddenly struck me ;-) Quote Link to comment https://forums.phpfreaks.com/topic/49088-pagination-help/#findComment-356764 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.