Hi all,
I have a search script which returns all employees from a database which sex is 'M'. Beacause there are too many results,
I implement it in a PREV 123456 NEXT style.
The problem looks like this >>
The script allows me to delete employee records from the database. If there are 20 results returned, then it will be presented on 5 pages. However, when I am in the last page(page 5) and I delete all the records on that page, I am then redirected to a success delete page. If now I hit the Back button, I am directed to page 5 again with no records displayed. This oly happens when I am in the last page, all other pages is ok.
How can I make the script direct me to page 4 which will show the records instead?
Below is my code for this portion. Any help will be most welcomed.
<?php
session_start();
Header("Cache-Control: no-cache, must-revalidate");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Fig. 29.6: arrays.php -->
<!-- Array manipulation -->
<!--<html xmlns = "http://www.w3.org/1999/xhtml"> -->
<html>
<head>
<title>Display All User Records</title>
</head>
<body>
<p style = "text-align: center">
Employee Record Found</p>
<table border = "1" cellpadding = "7" align = "center">
<tr>
<th>IC Number</th>
<th>Name</th><th>Address</th><th>Job Title</th>
<th>Date Join</th><th>Department</th><th>Sex</th><th></th>
</tr>
<?php
$db = mysql_connect("localhost","root","root");
if(!$db){
print("Could not connect to database. Please try again later.");
exit;
}
mysql_select_db("lms");
$limit=5;
$query = "SELECT * from emp WHERE (sex = 'M')";
$numresults = mysql_query($query);
$numrows = mysql_num_rows($numresults);
$offset = $_GET['offset'];
if (empty($offset)) {
$offset=0;
}
print("offset >> $offset");
$query = "SELECT * from emp WHERE (sex = 'M') order by icnum limit $offset,$limit";
$result = mysql_query($query);
$i=0;
while ($row=mysql_fetch_array($result)) {
$button_Value=htmlspecialchars(stripslashes($row["icnum"]));
$ic_array[$i]=$button_Value;
print( "<tr><form method = \"post\"
action = \"deleteRecByname.php\">
<td>" .(stripslashes($row["icnum"])). "</td>
<td>" .(stripslashes($row["empname"])). "</td>
<td>" .(stripslashes($row["address"])). "</td>
<td>" .(stripslashes($row["jobtitle"])). "</td>
<td>" .(stripslashes($row["datejoin"])). "</td>
<td>" .(stripslashes($row["dept"])). "</td>
<td>" .(stripslashes($row["sex"])). "</td>
<td><input type=\"checkbox\" name=\"checkbox$i\" value= \"checkbox\" /></td></tr>");
$i++;
}
print("<table width=\"75%\" border=\"0\">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<input type = \"submit\"value = \"Delete\" />
</tr>");
print("</form>");
$_SESSION['array_ic']=$ic_array;
if ($offset>1) {
$prevoffset=$offset-$limit;
print "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> \n";
}
$pages=intval($numrows/$limit);
print("$pages");
if ($numrows%$limit) {
$pages++;
}
for ($i=1;$i<=$pages;$i++) {
$newoffset=$limit*($i-1);
print "<a href=\"$PHP_SELF?offset=$newoffset\">$i</a> \n";
}
if (!(($offset/$limit)>=$pages-1) && $pages!=1) {
$newoffset=$offset+$limit;
print "<a href=\"$PHP_SELF?offset=$newoffset\">NEXT</a><p>\n";
}
$pg_num = intval($offset/$limit)+1;
print("You are at page $pg_num");
?>
</body>
</html>