数据表过滤器在应用SQL Server 2008分页代码时无法正常工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据表过滤器在应用SQL Server 2008分页代码时无法正常工作相关的知识,希望对你有一定的参考价值。
我设法使分页工作(感谢@zhorov)但不知何故它与搜索/过滤功能相冲突。
试图将每个查询放在每个$_post
中以查看选择查询是否不会发生冲突,当我将第一个选择查询放在底部时,它将覆盖查询的分页。当我将分页查询置于顶部时,它不会显示任何内容。
这是我的完整代码
`<?php
include('db.php');
include('function.php');
$query = '';
$output = array();
$query .= "SELECT * FROM users ";
if(isset($_POST["search"]["value"]))
{
$query .= "WHERE emp_id LIKE '%".$_POST["search"]["value"]."%' ";
$query .= "OR id LIKE '%".$_POST["search"]["value"]."%' ";
}
if(isset($_POST["order"]))
{
$query .= "ORDER BY ".$_POST['order']['0']['column']." ".$_POST['order']['0']['dir']." ";
}
else
{
$query .= "ORDER BY id DESC ";
}
if ($_POST["length"] != -1)
{
$query = "SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY id DESC) AS Rn
FROM users )";
$query .=
"sub WHERE status = 'Active' AND Rn BETWEEN ".($_POST["start"])." AND ".($_POST["start"] + $_POST["length"] - 1 );
}
$menu="";
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row['id'];
$sub_array[] = $row['emp_id'];
$sub_array[] = $row['username'];
$sub_array[] = $row['password'];
$sub_array[] = $row['email'];
$sub_array[] = $row['firstname'];
$sub_array[] = $row['middlename'];
$sub_array[] = $row['lastname'];
$sub_array[] = $row['location'];
$sub_array[] = $row['contact_no'];
$sub_array[] = $row['gender'];
$sub_array[] = $row['access_type'];
$sub_array[] = $row['status'];
$sub_array[] = $row['date_reg'];
$sub_array[] = '<button type="button" name="update" id="'.$row["id"].'" class="btn btn-warning btn-xs update">Deactivate</button>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
?>`
Pagination和Filter / Serch都应该有效。
答案
这个答案可能为时已晚,但我认为下一步可能有所帮助。你只需要将WHERE
子句放在子查询中。
<?php
...
# WHERE clause
$where = "WHERE (status = 'Active') ";
if (isset($_POST["search"]["value"])) {
$where .= " AND ((emp_id LIKE '%".$_POST["search"]["value"]."%') OR (id LIKE '%".$_POST["search"]["value"]."%')) ";
}
# Whole statement
if ($_POST["length"] != -1) {
$query = "SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY id DESC) AS Rn
FROM users ".$where." ) ";
$query .=
"sub WHERE Rn BETWEEN ".($_POST["start"])." AND ".($_POST["start"] + $_POST["length"] - 1 );
}
if (isset($_POST["order"])) {
$query .= " ORDER BY ".$_POST['order']['0']['column']." ".$_POST['order']['0']['dir']." ";
} else {
$query .= " ORDER BY id DESC ";
}
...
?>
笔记:
此示例基于您的代码。请注意,虽然您使用预准备语句($connection->prepare($query)
),但是当您有用户输入时,必须使用prevent SQL injection的参数。
以上是关于数据表过滤器在应用SQL Server 2008分页代码时无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章
在 SQL Server 2008 中区分两个表架构的最简单方法是啥?
MSSQL sql server 2005/2008 row_number()函数应用之–删除表中重
.NET Core EF框架使用SQL server 2008数据库分页问题:Incorrect syntax near 'OFFSET'. Invalid usage of the