JSHINER Posted April 15, 2007 Share Posted April 15, 2007 I'm using the following code, based on the PHP Freaks tutorial "Easy as PREV 1 2 3 NEXT": @mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER"); @mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB"); $limit = 5; $query_count = "SELECT count(*) FROM table"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT * FROM table ORDER BY date DESC LIMIT $limitvalue, $limit"; $result = mysql_query($query) or die("Error: " . mysql_error()); if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } $bgcolor = "#E0E0E0"; // light gray while($row = mysql_fetch_array($result)){ if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo 'Title:', $row['title'], ''; } if($page != 1){ $pageprev = $page--; echo("<a href=\"index.php?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?page=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"index.php?page=$i\">$i</a> "); } } if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page++; echo("<a href=\"index.php?page=$pagenext\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); } mysql_free_result($result); It works well, however it displays PREV5 1 NEXT 5 (text - not links) at the bottom instead of PREV 1 2 3 NEXT. When I type "index.php?page=2" it works, so I know there is a page 1,2,3 etc... however the links at the bottom are not working. Any thoughts ? Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted April 15, 2007 Share Posted April 15, 2007 I have no idea but I also had the same problem with this. Quote Link to comment Share on other sites More sharing options...
JSHINER Posted April 15, 2007 Author Share Posted April 15, 2007 Looking into this a little more - I have discovered by echoing "totalrows" that it is saying there is only 1 rows, when in fact there are 11 rows. So, what is wrong with my code here: $limit = 5; $query_count = "SELECT count(*) FROM table"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); $totalrows is not working. Quote Link to comment Share on other sites More sharing options...
JSHINER Posted April 16, 2007 Author Share Posted April 16, 2007 Got it all working. Nevermind Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted April 16, 2007 Share Posted April 16, 2007 would u mind posting the code? i have been having trouble with that tutorial myself. it would be greatly appreciated thanks HoTDaWg Quote Link to comment Share on other sites More sharing options...
JSHINER Posted April 16, 2007 Author Share Posted April 16, 2007 There were a few issues I found in the code: FIRST - starting at $limit - replace with this: $limit = 5; $query = "SELECT count(*) FROM table"; $count = mysql_query($query); $totalrows = mysql_result($count,0,0); The end result for $totalrows before was not working. SECOND - replace the PREV code with the following: if($page != 1){ $pageprev = $page-1; echo("<a class=\"nextprev\" href=\"index.php?page=$pageprev\">« PREV</a>"); }else{ echo("<span class=\"nextprev\">« PREV</span>"); } The $page-- needs to be $page-1 to work: THIRD - Same thing for NEXT as with PREV if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page+1; echo("<a class=\"nextprev\" href=\"index.php?page=$pagenext\">NEXT »</a>"); }else{ echo("<span class=\"nextprev\">NEXT »</span>"); } The $page++ needs to be $page+1 And that should do it. Let me know if you have any more problems with it. Quote Link to comment Share on other sites More sharing options...
phpbeginner Posted April 16, 2007 Share Posted April 16, 2007 Works like a charm ! 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.