Mr Chris Posted November 23, 2006 Share Posted November 23, 2006 Hi,I’m trying to put together a pagination script in php. The records get called out, but the next / previous buttons don’t <a href> despite there being over 25 entries and me asking the code to <a href>?I’m hoping it’s something blatently obvious someone can tell me!Many ThanksChris[code=php:0]<?php include('************'); $can_i_connect = db_connect(); // by db_connect function is in my include file if(!$can_i_connect) { echo "Could not connect to database"; } $limit = 10; $query_count = "SELECT count(*) FROM directory_listings"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count); if(empty($page)){ $page = 1; } $limitvalue = $page * $limit - ($limit); $query = "SELECT * FROM directory_listings 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 echo("<table>"); while($row = mysql_fetch_array($result)){ if ($bgcolor == "#E0E0E0"){ $bgcolor = "#FFFFFF"; }else{ $bgcolor = "#E0E0E0"; } echo("<tr bgcolor=".$bgcolor."><td>"); echo($row["company_name"]); echo("</td><td>"); echo($row["town"]); echo("</td></tr>"); } echo("</table>"); if($page != 1){ $pageprev = $page--; echo("<a href=\"test.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=\"test.php?page=$i\">$i</a> "); } } if(($totalrows % $limit) != 0){ if($i == $page){ echo($i." "); }else{ echo("<a href=\"text.php?page=$i\">$i</a> "); } } if(($totalrows - ($limit * $page)) > 0){ $pagenext = $page++; echo("<a href=\"test.php?page=$pagenext\">NEXT".$limit."</a>"); }else{ echo("NEXT".$limit); } mysql_free_result($result); ?> [/code] Link to comment https://forums.phpfreaks.com/topic/28220-pagination-help-please/ Share on other sites More sharing options...
esukf Posted November 23, 2006 Share Posted November 23, 2006 [code]$query_count = "SELECT count(*) FROM directory_listings"; $result_count = mysql_query($query_count); $totalrows = mysql_num_rows($result_count);[/code]You using the aggregrate COUNT in the select statement which will returns one row with the value of count.Use the following to just get the count value:-[code]$query_count = "SELECT count(*) FROM directory_listings"; $result_count = mysql_query($query_count) or die("Error: " . mysql_error()); $totalrows = mysql_result($result_count, 0); [/code] Link to comment https://forums.phpfreaks.com/topic/28220-pagination-help-please/#findComment-129088 Share on other sites More sharing options...
Jenk Posted November 23, 2006 Share Posted November 23, 2006 or just remove that section all together and use mysql_num_rows() on the result set that contains the actual data.. just like it was designed to do.. Link to comment https://forums.phpfreaks.com/topic/28220-pagination-help-please/#findComment-129108 Share on other sites More sharing options...
xyn Posted November 23, 2006 Share Posted November 23, 2006 if that was the tutorial from phpfreaks, i've noticed there was a problem withthe first query where is uses 'COUNT(*)' change....$limit = 10; $query_count = "SELECT count(*) FROM directory_listings";To this...$limit = 10; $query_count = "SELECT * FROM directory_listings"; Link to comment https://forums.phpfreaks.com/topic/28220-pagination-help-please/#findComment-129109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.