yandoo Posted September 13, 2007 Share Posted September 13, 2007 Hi, i ws hoping for a little help with my php if possible. I have created a guestbook page that pulls records from a database and displays them, along with the PREV and NEXT links (& page numbers between them). All seems to be worknig correctly except for 2 things!!........ Firstly the NEXT link text is hyperlinked even whn the user is at the last page! so they end up able to continuously being able to go to the next page, even though there are no records! Secondly the page numbers (in between PREV NEXT) doont seem to go up enough to the amount of pages worth of records!! I have tested knowing that there are 5 pages worth of records and when i get to page 4....there is no page 5 displayed...if i click NEXT however, iam taken to the next set of records (page 5) which 's page number was not displayed!!!! I have tried to do some debugging to do with the code refering to the NEXT hyperlink and after echoing out to see whast was working or not i found that the variable $totalrows which is associated with a record_count query always would return the value of 1???? Even when there were clearly 21 rows worth of data there!!!!!????? Could this be part ofthe problem??? These are the to areas that im really stuck on, i could use and im at my witz end now Please help?!! Thank You code below: <?php $dbservertype='mysql'; $servername='localhost'; // username and password to log onto db server $dbusername='root'; $dbpassword=''; // name of database $dbname='lopesarms'; connecttodb($servername,$dbname,$dbusername,$dbpassword); function connecttodb($servername,$dbname,$dbuser,$dbpassword) { global $link; $link=mysql_connect ("$servername","$dbuser","$dbpassword"); if(!$link){die("Could not connect to MySQL");} mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); } error_reporting(E_ALL); ini_set('display_errors', '1'); $limit = 4; $query_count = "SELECT count(*) FROM guest_book"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT * FROM guest_book LIMIT $limitvalue, $limit"; $result = mysql_query($query) or die("Error: " . mysql_error()); echo"$totalrows"; if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } $bgcolor = "#E0E0E0"; // light gray echo("<table>"); while($row = mysql_fetch_array($result)){ if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo("<tr bgcolor=".$bgcolor."><td>"); echo($row["name"]); echo($row["email"]); echo("</td></tr>"); } echo("</table>"); echo"<font size=2>"; $pageprev = $page-1; if($page != 1){ echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a> "); }else{ echo("PREV"." "); } $numofpages = $limit / $totalrows; for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } if(( $limit % $totalrows) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } $pagenext = $page+1; //$new = ($limit * $page) - $totalrows; //echo"$new"; if(($limit * $page) - ($totalrows) > 0){ echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>"); }else{ echo("NEXT"); } echo"</font>"; mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/ Share on other sites More sharing options...
sasa Posted September 13, 2007 Share Posted September 13, 2007 try <?php $dbservertype='mysql'; $servername='localhost'; // username and password to log onto db server $dbusername='root'; $dbpassword=''; // name of database $dbname='lopesarms'; connecttodb($servername,$dbname,$dbusername,$dbpassword); function connecttodb($servername,$dbname,$dbuser,$dbpassword) { global $link; $link=mysql_connect ("$servername","$dbuser","$dbpassword"); if(!$link){die("Could not connect to MySQL");} mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error()); } error_reporting(E_ALL); ini_set('display_errors', '1'); $limit = 4; $query_count = "SELECT count(*) FROM guest_book"; $result_count = mysql_query($query_count); //$totalrows = mysql_num_rows($result_count); <-- change this line $totalrows = mysql_result($result_count, 0, 0); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT * FROM guest_book LIMIT $limitvalue, $limit"; $result = mysql_query($query) or die("Error: " . mysql_error()); echo"$totalrows"; if(mysql_num_rows($result) == 0){ echo("Nothing to Display!"); } $bgcolor = "#E0E0E0"; // light gray echo("<table>"); while($row = mysql_fetch_array($result)){ if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo("<tr bgcolor=".$bgcolor."><td>"); echo($row["name"]); echo($row["email"]); echo("</td></tr>"); } echo("</table>"); echo"<font size=2>"; $pageprev = $page-1; if($page != 1){ echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a> "); }else{ echo("PREV"." "); } //$numofpages = $limit / $totalrows; <-- change this line $numofpages = ceil($totalrows /$limit); for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } /* remove this part if(( $limit % $totalrows) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); } } */ $pagenext = $page+1; //$new = ($limit * $page) - $totalrows; <-- and remove this part //echo"$new"; if($page <= $numofpages) { echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>"); }else{ echo("NEXT"); } echo"</font>"; mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/#findComment-347538 Share on other sites More sharing options...
yandoo Posted September 13, 2007 Author Share Posted September 13, 2007 Thats super Thank you Sasa it works a charm! Delighted an Delerious now! Quote Link to comment https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/#findComment-347642 Share on other sites More sharing options...
yandoo Posted September 13, 2007 Author Share Posted September 13, 2007 Hi, there is just one thing that im a little unsure of though.... The NEXT hyperlink now disappears as it should do at the end of the page. But only 1 page after the last page....??? For example i when there are only 6 pages worth of records, when on page 6 the NEXT hyperlink is still there....???? If its clicked your taken to page 7's worth of records (which is empty as there are none). But then the NEXT pagnation text is NOT hyperlinked!! So it is working almost perfeclty but not quite. Any help me greatly appreciated, especially you sasa coz your a genious ! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/#findComment-347724 Share on other sites More sharing options...
sasa Posted September 13, 2007 Share Posted September 13, 2007 ups change line if($page <= $numofpages) { to if($page < $numofpages) { before echo NEXT link Quote Link to comment https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/#findComment-347801 Share on other sites More sharing options...
yandoo Posted September 13, 2007 Author Share Posted September 13, 2007 Thats Excellent! Thank You Sasa Quote Link to comment https://forums.phpfreaks.com/topic/69124-solved-little-pagnation-help-please/#findComment-347818 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.