I have been on this for 3 days now without much progress. I am writing a web address book using php and mysql. I succeeded in displaying the entire contents of the database on a page. I was also able to limit the number of rows displayed to 20. However when I click the link to the next page, the same records are displayed. I will really be happy for help ASAP.
The code:
<?php
$limit = 20;
if (isset($GET["page"])){
$page = $GET["page"];
}else{
$page = 1;
}
$username = "username";
$password ="mypass";
$database = "mydb";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query = "SELECT FROM phonebk";
$result=mysql_query($query)or die("can't execute query");
$total_items=mysql_num_rows($result);
$total_pages = ceil($total_items / $limit);
$set_limit=$page $limit - ($limit);
$query = mysql_query("SELECT * from phonebk LIMIT $set_limit, $limit");
while ($results = mysql_fetch_array($query)) {
// get results
$id = $results["id"];
$sname = $results["sname"];
$fname = $results["fname"];
echo "$sname"." ".$fname."<br>";
}
$prev_page = $page - 1;
if ($prev_page >=1) {
echo ("<b><</b> <a href = page2.php?limit=$limit;page=$prev_page><b>Prev</b></a>");
}
for ($a = 1; $a <= $total_pages; $a++)
{
if ($a == $page) {
echo ("<b> $a</b> | "); // no link
} else {
echo ("<a href=page2.php?limit=$limit&;page=$a> $a</a> | ");
}
}
$next = $page + 1;
if ($next <=1) {
echo ("<b><</b> <a href=page2.php?limit=$limit;page=$prev_page><b>Prev</b></a>");
}
?>