I've implemented a search function that displays all records and when I search it will filter all with pagination.
There's a problem, though: every time I search for a record, it displays all pages, but when I click the second page, it will display all records in database - not what I want to filter.
Here's my code:
<form id="form1" name="form1" method="get" action="">
<br/>
<div>
<font color ="white" face ="Arial" size ="5" style="padding: 5px; text-align: center;">Search for an Applicant Record</font><br/><br/>
<table border=0 cellSpacing=2 cellPadding=2 align ="left" >
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><a href="search.php"><img src="/applicants/icons/reset.png" title="Reset Page"/><font size="3" face="Tahoma" color="white"><b>Reset</b></font></a></td>
</tr>
<tr>
<td> </td>
<td align="left"><p><label for="searchfor"><font color ="white" size ="2" face ="Tahoma"><b>Search for: </b></font></label></p></td>
<td> </td>
<td align="left"><input type="text" name="searchfor" class="tb5a" id="searchfor" size="25" /></td>
<td align="left">
<select name="select" class="tb5a">
<option selected="selected">Applicant's Name</option>
<option>Position Applying For</option>
<option>Work Experience</option>
<option>Remarks</option>
</select>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="left"><p><label for="from"><font color ="white" size ="2" face ="Tahoma"><b>Date Applied from: </b></font></label></p></td>
<td> </td>
<td align="left"><input name="from" type="text" class="tb5a" id="from" size="10" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="left"><p><label for="date_appliedto"><font color ="white" size ="2" face ="Tahoma"><b> to: </b></font></label></p></td>
<td> </td>
<td align="left"><input name="to" type="text" class="tb5a" id="to" size="10" /></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="1" align="right"><p><input type="submit" name="search" class="update" id="button" value=" Search " onclick=verify();/></p></td>
</tr>
</table>
</div>
</form>
<?php
error_reporting(0);
include_once 'include/conn.inc.php';
//Include the PS_Pagination class
include('include/ps_pagination.php');
//Connect to mysql db
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('timemanagement', $conn);
if(!$status) die("Failed to select database!");
if (isset($_POST['search'])) {
$sql = "Select * from applicants_access order by applicant_id desc";
}else{
if ($_REQUEST["searchfor"]<>'') {
$search_searchfor = " AND (lname LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%' OR fname LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%' OR mname LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%' OR image LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%' OR appfor LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%' OR workexp LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%' OR remarks LIKE '%".mysql_real_escape_string($_REQUEST["searchfor"])."%')";
}
if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>'') {
$sql = "SELECT * FROM applicants_access WHERE date_applied >= '".mysql_real_escape_string($_REQUEST["from"])."' AND date_applied <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_searchfor;
}
else if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM applicants_access WHERE date_applied >= '".mysql_real_escape_string($_REQUEST["from"])."'".$search_searchfor;
}
else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM applicants_access WHERE date_applied <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_searchfor;
}
else {
$sql = "SELECT * FROM applicants_access WHERE applicant_id>0".$search_searchfor;
}
if (($_REQUEST["select"])==($_REQUEST["searchfor"])){
$sql = "Select * from applicants_access order by applicant_id desc";
}
$sql_result = mysql_query($sql);
echo '<center>';
/*
* Create a PS_Pagination object
*
* $conn = MySQL connection object
* $sql = SQl Query to paginate
* 10 = Number of rows per page
* 5 = Number of links
* "param1=valu1¶m2=value2" = You can append your own parameters to paginations links
*/
$pager = new PS_Pagination($conn, $sql, 20, 10);
/*
* Enable debugging if you want o view query errors
*/
$pager->setDebug(true);
/*
* The paginate() function returns a mysql result set
* or false if no rows are returned by the query
*/
$rs = $pager->paginate();
if(!$rs) die(mysql_error());
if($sql_result){
echo '<br/>',
'<div align ="center">',
'<table id="table-2" cellspacing="0" cellpadding="1" border=1 >',
'<thead>',
'<th><center><font color ="#640303"><b>Last Name</b></font></center></th>',
'<th><center><font color ="#640303"><b>First Name</b></font></center></th>',
'<th><center><font color ="#640303"><b>Middle Name</b></font></center></th>',
'<th><center><font color ="#640303"><b>View Attachment</b></font></center></th>',
'<th><center><font color ="#640303"><b>Contact Number</b></font></center></th>',
'<th><center><font color ="#640303"><b>Position Applying For</b></font></center></th>',
'<th><center><font color ="#640303"><b>Work Experience</b></font></center></th>',
'<th><center><font color ="#640303"><b>Date Applied</b></font></center></th>',
'<th><center><font color ="#640303"><b>Remarks</b></font></center></th>',
'</thead>';
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_array($rs)) {
echo '<tbody>',
'<tr>',
'<td><center><font color = "black">', $row["lname"], '</font></center></td>',
'<td><center><font color = "black">', $row["fname"], '</font></center></td>',
'<td><center><font color = "black">', $row["mname"], '</font></center></td>',
'<td><center><a href=images/'.$row[0].'/'.$row['filename'].' target="_blank">View</a></td>',
'<td><center><font color = "black">', $row["cnumber"], '</font></center></td>',
'<td><center><font color = "black">', $row["appfor"], '</font></center></td>',
'<td><center><font color = "black">', $row["workexp"], '</font></center></td>',
'<td><center><font color = "black">', $row["date_applied"], '</font></center></td>',
'<td><center><font color = "black">', $row["remarks"], '</font></center></td>',
'</tr></tbody>';
}
}
else {
echo '<tr><td colspan="45">No results found</td></tr>';
}
echo '</table></div>';
?>
<?php
}
}
echo '<div class="paginationTG">';
echo '<ul>';
//Display the link to first page: First
echo $pager->renderFirst();
//Display the link to previous page: <<
echo $pager->renderPrev();
echo'</span>';
/*
* Display page links: 1 2 3
* $prefix = Will be prepended to the page link (optional)
* $suffix = Will be appended to the page link (optional)
*
*/
echo $pager->renderNav('<li class="currentpage">', '</li>');
//Display the link to next page: >>
echo $pager->renderNext();
//Display the link to last page: Last
echo $pager->renderLast();
echo '</div>' . '</ul>';
echo '</center>';
?>