I made a PHP search engine with multiple fields, every thing is fine and it works super but I need pagination and I want to ask, how can I create pagination for my search engine form and how can I show all record in one click.
Here is my code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>search</title>
<link rel="stylesheet" type="text/css"href="http://www.sayahonline.com/a/my.css">
<link href="http://www.sayahonline.com/templates/sayah/favicon.ico" rel="shortcut icon">
</head>
<body>
<center>
<form name="search" method="post" action="<?=$PHP_SELF?>">
<Select NAME="field">
<Option VALUE="idno">ID</option>
<Option VALUE="name">Name</option>
<Option VALUE="fname">Father Name</option>
<Option VALUE="province">Province</option>
</Select>
<input placeholder="Search" type="text" name="find" />
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
<table border="1" width="">
<thead>
<tr>
<th><center>ID</center></th>
<th><center>Name</center></th>
<th><center>Father Name</center></th>
<th><center>G/F Name</center></th>
<th><center>School</center></th>
<th><center>Province</center></th>
<th><center>Grade</center></th>
<th><center>Result</center></th>
</tr>
</thead>
<tbody id="tbl">
<p></p>
<?
//get information
$field = @$_POST['field'] ;
$find = @$_POST['find'] ;
$searching = @$_POST['searching'] ;
//once submitted
if ($searching =="yes")
{
echo "<p></p>";
//blank search returns all results
mysql_connect("localhost","User","Password") or die(mysql_error());
mysql_select_db("Name") or die(mysql_error());
mysql_query('SET NAMES utf8');
//filter search term
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//search database
$getquery = mysql_query("SELECT * FROM Table WHERE upper($field) LIKE'%$find%' LIMIT $start, $per_page");
//display the results
while($result = mysql_fetch_assoc($getquery))
{
$idno = $result ['idno'];
$name = $result ['name'];
$fname = $result ['fname'];
$gfname = $result ['gfname'];
$school = $result ['school'];
$province = $result ['province'];
$grade = $result ['grade'];
$result = $result ['result'];
echo "
<tr>
<td><center>$idno</center></td>
<td>$name</td>
<td>$fname</td>
<td>$gfname</td>
<td>$school</td>
<td>$province</td>
<td><center>$grade</center></td>
<td>$result</td>
</tr>
</center>
";
}
//number of results or error
$anymatches=mysql_num_rows($getquery);
if ($anymatches == 0)
{
echo "Sorry...";
}
//the search term
echo "Result: <b>$find</b> | Total: <b>$anymatches</b> <hr size='1'>";
}
?>
</body>
</html>
What you need to do is revise your query for the search. 1) Search for the query criteria. 2) Limit the query to say, 50 or so. 3) Store the query limit in a session. 4) Create a "next 50" and "previous 50" buttons. 5) When they are clicked run the query again and use the session limit as a starting pointer in the query. 6) Go to step 2.
Hope that helps.
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
Just retrieve data from the database according to the current page.