搜索数据并将数据保存在字段中
search data and keep data in Field
请查看此代码以解决问题。
- 当我按姓名或年龄搜索数据时,我会在页面的前10行中得到结果,但当我转到下一页查看结果时,它会返回到所有结果。我如何在没有我的数据的情况下转到下一页?我输入的数据是姓名或年龄而没有删除或者我在里面搜索的页面
- 当我搜索所有数据时,如果age为null,我在age中的null列没有出现问题,我需要查看所有数据,如果它为null或否,我输入类型int表示age
<table class="tab">
<thead>
<tr>
<th colspan="5" class="table-title">
<h3>Search</h3>
</th>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="searchform">
<tr class="trr">
<th class="thh">
Full Name
</th>
<th class="thh">
Age
</th>
</tr>
</thead>
<tbody>
<tr class="trr">
<td class="tdd">
<input type="text" autocomplete="off" style="width:150px;" name="name">
</td>
<td class="tdd">
<input type="text" autocomplete="off" style="width:50px;" name="age">
</td>
<td class="tdd">
<input type="submit" name="submit" value="Search">
</td>
</tbody>
</table>
</form>
<br>
</body>
<?php
if (!empty($_POST['name'])){
$name=$_POST['name'];
}
if (!empty($_POST['age'])){
$age=$_POST['age'];
}
$con=mysqli_connect("localhost","name","password","db");
$ch = 'SET CHARACTER SET utf8';
mysqli_query($con,$ch);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$tbl_name="table"; //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$resul = mysqli_query($con,"SELECT ID,full_name,age FROM arrested WHERE
full_name LIKE '%$name%' AND
age LIKE '%$age%'");
$num_rows = mysqli_num_rows($resul);
$total_pages =$num_rows;
/* Setup vars for query. */
$targetpage = "detainees.php"; //your file name (the name of this file)
$limit = 10;
//how many items to show per page
if(isset($_GET['page']))
{$page = $_GET['page'];}
else{$page =0;}
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class='"pagination'">";
//previous button
if ($page > 1)
$pagination.= "<a href='"$targetpage?page=$prev'">Previous</a>";
else
$pagination.= "<span class='"disabled'">Previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class='"current'">$counter</span>";
else
$pagination.= "<a href='"$targetpage?page=$counter'">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class='"current'">$counter</span>";
else
$pagination.= "<a href='"$targetpage?page=$counter'">$counter</a>";
}
$pagination.= "<b class='dot'> . . . </b>";
$pagination.= "<a href='"$targetpage?page=$lpm1'">$lpm1</a>";
$pagination.= "<a href='"$targetpage?page=$lastpage'">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href='"$targetpage?page=1'">1</a>";
$pagination.= "<a href='"$targetpage?page=2'">2</a>";
$pagination.= "<b class='dot'> . . . </b>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class='"current'">$counter</span>";
else
$pagination.= "<a href='"$targetpage?page=$counter'">$counter</a>";
}
$pagination.= "<b class='dot'> . . . </b>";
$pagination.= "<a href='"$targetpage?page=$lpm1'">$lpm1</a>";
$pagination.= "<a href='"$targetpage?page=$lastpage'">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href='"$targetpage?page=1'">1</a>";
$pagination.= "<a href='"$targetpage?page=2'">2</a>";
$pagination.= "<b class='dot'> . . . </b>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class='"current'">$counter</span>";
else
$pagination.= "<a href='"$targetpage?page=$counter'">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href='"$targetpage?page=$next'">Next</a>";
else
$pagination.= "<span class='"disabled'">Next</span>";
$pagination.= "</div>'n";
}
/* Get data. */
$resul = mysqli_query($con,"SELECT ID,full_name,age FROM arrested WHERE
full_name LIKE '%$name%' AND
age LIKE '%$age%'
ORDER BY ID ASC LIMIT $start, $limit");
echo'<table class="table-fill">';
echo'<thead><tr class="data">
<th class="text-left">ID</th>
<th class="text-left">Full Name</th>
<th class="text-left">Age</th>
</tr></thead>
<tbody class="table-hover">';
while($x= mysqli_fetch_object($resul))
{
echo '<tr class="data">';
echo '<td class="data">';
echo $x->ID;
echo "</td>";
echo '<td class="data">';
echo $x->full_name;
echo "</td>";
echo '<td class="data">';
echo $x->status;
echo "</td>";
echo "</tr>";
}
print "</table>";
mysqli_close($con);
?>
<?=$pagination ?>
使用会话($_SESSION),如本例所示:
<?php
session_start();
if (isset($_POST['var']) && !empty($_POST['var']))
{
$var = $_POST['var'];
$_SESSION['var'] = $var;
}
elseif (isset($_SESSION['var']))
{
$var = $_SESSION['var'];
}
?>
在$_SESSION中设置的变量将一直保留到会话结束(通常是在浏览器关闭时)。
相关文章:
- SIMPLE Javascript代码,用于显示谷歌电子表格中单个字段的数据
- 单击鼠标,用MySQL数据填充html表单输入字段
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 基于javascript中的状态字段对动态行数据进行动态着色
- 有了字段的名称,我如何用空白数据初始化对象的未定义字段
- javascript绑定另一个select2输入字段rails的select2 inputonchange事件的数据
- 在Drupal7中扩展字段集时,修改collapse.js以从xml获取附加数据
- 本地存储自动完成/提前键入字段的数据
- 在输入字段中选择一个值,然后用相应的数据填充另一个字段
- 从包含ng的字段中获取表单数据,而无需使用ng模型
- 如何从一个表单的输入字段中获取数据,并使用JQuery在不同的表单中填充相同的输入类型
- 如何在页面加载时直接使用Javascript在输入字段中填写数据
- AngularJs的Kendo UI:当用户在网格中选择行时,如何在文本框字段上绑定数据
- 从Node.js中的JSON数据中提取JSON字段
- 在Meteor中加载带有采集数据的选择输入字段
- 生成动态表单输入字段,并在angularJS中以数组形式收集字段数据
- 如何检测从数据列表中选择时更改的输入字段
- 传递数据以填充在ajax Modal中加载的输入字段
- 如何使用“数据”传递数据?字段
- 如何获取“数据”字段从xhr.responseText