我正在尝试使用分页进行搜索和排序,该页面显示数据库中的表。最初页面显示数据库中的所有记录以及正常工作的分页。但是当我实现搜索和排序时,只有第一页显示搜索/排序结果。其余页面显示数据库中的所有记录。这是我的代码:
<?php
/* code for pagination */
$no_of_record_per_page=2;
if(isset($_POST['btn_search']))
{
$search_term = $_POST['txt_search_term'];
$select_query = "select * from tbl_admin where admin_name like '%$search_term%'";
}
elseif (isset($_POST['btn_asc']))
{
$sort_by = $_POST['ddl_sort_by'];
$select_query = "select * from tbl_admin order by $sort_by asc";
}
elseif (isset($_POST['btn_desc']))
{
$sort_by = $_POST['ddl_sort_by'];
$select_query = "select * from tbl_admin order by $sort_by desc";
}
else
{
$select_query = "select * from tbl_admin";
}
$result = mysql_query($select_query);
$total_record=mysql_num_rows($result);
$max_page=ceil($total_record/$no_of_record_per_page);
if(isset($_GET['page']))
{
$page=$_GET['page'];
$next_page = $page+1;
$prev_page = $page-1;
if($next_page>$max_page)
{
$next_page = $max_page;
}
if($prev_page<1)
{
$prev_page=1;
}
}
else
{
$page=1;
$prev_page=1;
$next_page=2;
if($next_page > $max_page)$next_page = $max_page;
}
$limit_page=$page-1;
$limit = " LIMIT ".$no_of_record_per_page*$limit_page.",".$no_of_record_per_page;
?>
有一个用于搜索的文本框和用于排序的按钮和下拉列表。 这是显示记录的代码。
<?php
$select_query = $select_query.$limit;
$result = mysql_query($select_query);
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td class="center"><?php echo $row['admin_id']; ?></td>
<td class="center"><?php echo $row['admin_clg_id']; ?></td>
<td class="center"><?php echo $row['admin_name']; ?></td>
<td class="center"><?php echo $row['admin_mobile_no']; ?></td>
<td class="center"><?php echo $row['admin_uname']; ?></td>
</tr>
<?php
}
?>
任何人都可以识别我的错误。我是初学者。谢谢 (我也知道数据表和类似的插件,但我想学习自己实现一次。所以请不要建议任何插件。)