PHP Search + pagination, single-write easy, combination is not easy, online find a bit, code is not complete, search and pagination is not fully implemented, through their own attempts to modify, successful implementation of simple search + paging function.
The effect is as follows:
Here's the complete code! Write it on a PHP page! The changes you need to make are your database information and table information. can achieve the same effect of the above picture! Thanks to online sharing of netizens, I on this basis to change the function I want, so also posted out, want to help some friends.
<?php
$wherelist =array ();
$urlist =array ();
if (!empty ($_get[' id '))
{
$wherelist []= ' id like '% '. $_get[' id ']. " %‘";
$urllist []= "id=". $_get[' id '];
}
if (!empty ($_get[' username '))
{
$wherelist []= "username like '%". $_get[' username ']. " %‘";
$urllist []= "username=". $_get[' username '];
}if (!empty ($_get[' age '))
{
$wherelist []= "age-like '%". $_get[' age '). " %‘";
$urllist []= "age=". $_get[' age ');
}
$where = "";
if (count ($wherelist) >0)
{
$where = "where". Implode (' and ', $wherelist);
$url = ' & ' Implode (' & ', $urllist);
}
The implementation principle of paging
1. Get the total number of records in the data table
$mysql _server_name= ' localhost ';
$mysql _username= ' root ';
$mysql _password= ' root ';
$conn =mysql_connect ($mysql _server_name, $mysql _username, $mysql _password);
mysql_query ("Set names ' UTF8 '");
mysql_select_db ("AAA");
$sql = "SELECT * from user $where";
$result =mysql_query ($sql);
$totalnum =mysql_num_rows ($result);
Show number of bars per page
$pagesize = 5;
A total of a few pages
$maxpage =ceil ($totalnum/$pagesize);
$page =isset ($_get[' page ')? $_get[' page ']:1;
if ($page <1)
{
$page = 1;
}
if ($page > $maxpage)
{
$page = $maxpage;
}
$limit = "Limit". ($page-1) * $pagesize. ", $pagesize";
$sql 1= "SELECT * from the user {$where} ORDER by id desc {$limit}"; The ID descending is added here
$res =mysql_query ($sql 1);
?>
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> User profile Display </title>
<body>
<form action= "searchpage.php" method= "Get" >
Id:<input type= "text" name= "id" value= "<?php echo $_get[' id ']?>" size= "8" >
User name <input type= "text" name= "username" value= "<?php echo $_get[' username ']?>" size= "8" >
Age: <input type= "text" name= "aged" value= "<?php echo $_get[' ages ']?>" size= "8" >
<input type= "button" value= "View All" onclick= "window.location= ' searchpage.php '" >
<input type= "Submit" value= "Search" >
</form>
<br/>
<table border= "1" width= ">"
<tr>
<td> numbering </td>
<td> User name </td>
<td> Age </td>
<td> Sex </td>
<td> Telephone </td>
<td> Address </td>
</tr>
<?php while ($row = Mysql_fetch_assoc ($res)) {?>
<tr>
<td><?php echo $row [' ID ']?></td>
<td><?php echo $row [' username ']?></td>
<td><?php echo $row [' age ']?></td>
<td><?php if ($row [' sex ']) {echo ' Man ';} Else{echo ' woman ';}?></td>
<td><?php echo $row [' phone ']?></td>
<td><?php echo $row [' address ']?></td>
</tr>
<?php}?>
<tr>
<TD colspan= "6" >
<?php
echo "Current {$page}/{$maxpage} page total {$totalnum} bar";
echo "<a href= ' searchpage.php?page=1{$url} ' > Home </a>";
echo "<a href= ' searchpage.php?page=". ($page-1). " {$url} ' > previous </a> ';
echo "<a href= ' searchpage.php?page=". ($page + 1). " {$url} ' > Next </a> ';
echo "<a href= ' searchpage.php?page={$maxpage} {$url} ' > Last </a>";
?>
</td>
</tr>
</table>
</body>
PHP for search and paging effects-pro-Test effective