Greetins all!
I am trying to do some pagination w/ php and mysql. I got the basics of it hammered out, but I can't get the prev 1 2 3 next link to work. I think I may not be using the
{$_SERVER['PHP_SELF']}
command correctly. Please take a look over my code and see if there is something else that I need to do. Thank you in advance.
Code Start:
<?php
require_once('Connections/mysqlconn.php');
$limit = 10;
$query_count = "SELECT count(*) FROM herbs";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM herbs 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>");
//alternate color workout
while($row = mysql_fetch_array($result)){
if ($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}
//build table rows
echo("<tr bgcolor=".$bgcolor."><td>");
echo($row["herb"]);
echo("</td><td>");
echo($row["usage"]);
echo("</td><td>");
echo($row["thumb"]);
echo("</td></tr>");
}
echo("</table>");
//on first page or not
if($page != 1){
$pageprev = $page--;
echo("<a href={$_SERVER['PHP_SELF']}?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={$_SERVER['PHP_SELF']}?page=$i>".$i."</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href={$_SERVER['PHP_SELF']}?page=$i>$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page + 1;
echo("<a href={$_SERVER['PHP_SELF']}?page=$pagenext>NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
mysql_free_result($result);
?>