Hey everyone,
This is my first attempt at pagination. I seem to have everything working pretty well except for one slight problem.
Example: If I search for a PO Number containing the number 10. My query returns all the correct information along with the correct pagination links, however, Let's say I'm on page 6 of a current search. Now I type in a new term to search for such as PO Number containing '11', it returns the results - but, it automatically starts the new results on page 6 since that is the page I was on from the last search. I am still very much in the learning stage of php, and I don't know how to clear the $currentpage variable on a new search.
Here is code:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Search For A PO</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<div id="static">
<div class="static">
<a href="http://mysite/">Home</a> </div>
</div>
<div id="static2">
<div class="static2">
<a href="http://mysite/insert.php">Create A New PO</a> </div>
</div>
<body>
<div id="content">
<div class="contentbox">
<h1>Search For A PO</h1>
<?php
$type = $_REQUEST['type'];
$term = $_REQUEST['term'];
?>
<form method="post" action="">
<input type="hidden" name="action" value="search"/>
<table class="search">
<tr><td> Search: <select name="type">
<option value="po_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='po_num'){print "selected";}?>>PO Number</option>
<option value="date" <?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='date'){print "selected";}?>>Date</option>
<option value="vin_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='vin_num'){print "selected";}?>>VIN Number</option>
</select></td>
<td>for: <input type="text" name="term" /><br /></td></tr>
<tr><td> <input type="submit" name="submit" value="Search" /></td>
<td class="date">* date format: yyyymmdd</td></tr>
</table>
</form>
</div>
<hr width='100%'></hr>
<?php
if ($term == '')
{
echo '<h2>Please enter a value</h2>';
exit;
}
if(!strcmp($_REQUEST['action'],"search")) {
// database connection info
$conn = mysql_connect('localhost','xxx','xxx') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('xxx',$conn) or trigger_error("SQL", E_USER_ERROR);
//safe strings
$safetype = mysql_real_escape_string($type);
$safeterm = trim(mysql_real_escape_string($term));
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM parts_ord where $safetype like '%$safeterm%' AND date<>'0'";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 2;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "select * from parts_ord where $safetype like '%$safeterm%' AND date<>'0' ORDER BY date DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
echo '<h2>Search Results: '.$numrows.' matches found.</h2>';
while ($row = mysql_fetch_assoc($result)){
//var_dump($row);
//var_dump($offset);
echo "<br/><table class='results'>
<tr><td class='short'><B>PO Number:</B> </td><td>".$row['po_num']."</td></tr>
<tr><td><B>Vendor:</B> </td><td>".$row['vendor']."</td></tr>
<tr><td><B>Date:</B> </td><td>".$row['date']."</td></tr>
<tr><td><B>VIN Number:</B> </td><td>".$row['vin_num']."</td></tr>
<tr><td><B>Description:</B> </td><td>".$row['descr']."</td></tr>
<tr><td><B>Invoice Number:</B> </td><td>".$row['inv_num']."</td></tr>
<tr><td><B>Purchase Agent:</B> </td><td>".$row['agt']."</td></tr>
<tr><td colspan='2'><a href=\"edit.php/?id=".$row['po_num']."\">Edit</a></td></tr></table>";
}
if ($numrows==0){
exit;
}
else{
/****** build the pagination links ******/
// range of num links to show
$range = 10;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&term=$term&type=$type&action=search'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&term=$term&type=$type&action=search'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&term=$term&type=$type&action=search'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&term=$term&type=$type&action=search'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&term=$term&type=$type&action=search'>Last</a> ";
} // end if
/****** end build pagination links ******/
}
}
?>
<br/>
<hr width='100%'></hr>
</div>
</body>
</html>
Any Help is very much appreciated.