应用 SQL Server 2008 分页代码时数据表过滤器不起作用
Datatables Filter not working when applying SQL Server 2008 pagination code
我已经设法使分页工作(感谢 @zhorov),但不知何故它与 search/filter 功能冲突。
尝试将每个查询放在每个 $_post
中以查看 select 查询是否不会冲突,当我将第一个 select 查询放在底部时它会覆盖查询用于分页。当我将分页查询放在顶部时,它不会显示任何内容。
这是我的完整代码
`<?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);
?>`
分页和 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。
我已经设法使分页工作(感谢 @zhorov),但不知何故它与 search/filter 功能冲突。
尝试将每个查询放在每个 $_post
中以查看 select 查询是否不会冲突,当我将第一个 select 查询放在底部时它会覆盖查询用于分页。当我将分页查询放在顶部时,它不会显示任何内容。
这是我的完整代码
`<?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);
?>`
分页和 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。