gpt4 book ai didi

javascript - 数据表+服务器端处理+搜索过滤

转载 作者:行者123 更新时间:2023-11-30 09:50:26 27 4
gpt4 key购买 nike

我正在学习本教程:
http://coderexample.com/datatable-demo-server-side-in-phpmysql-and-ajax/

这是演示:
http://coderexample.com/demo/datatable-demo-server-side-in-phpmysql-and-ajax/

如果我在搜索输入中搜索 ou,我会得到 No matching records found 但我想返回的是这一行 Airi Satou至少。

this是我必须更改的代码,我认为,因为我必须在搜索服务器端进行更改。

<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "Password1";
$dbname = "test";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* Database connection end */


// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;


$columns = array(
// datatable column index => database column name
0 =>'employee_name',
1 => 'employee_salary',
2=> 'employee_age'
);

// getting total number records without any search
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.


if( !empty($requestData['search']['value']) ) {
// if there is a search parameter
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$sql.=" WHERE employee_name LIKE '".$requestData['search']['value']."%' "; // $requestData['search']['value'] contains search parameter
$sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR employee_age LIKE '".$requestData['search']['value']."%' ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); // again run query with limit

} else {

$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

}

$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();

$nestedData[] = $row["employee_name"];
$nestedData[] = $row["employee_salary"];
$nestedData[] = $row["employee_age"];

$data[] = $nestedData;
}



$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);

echo json_encode($json_data); // send data as json format

?>

我说这是我必须更改的代码是否正确?
如果是这样,谁能建议我必须做什么?
我知道这个问题很多,但非常感谢您的指导!

最佳答案

 $sql.=" WHERE employee_name LIKE '".$requestData['search']['value']."%' "; 

将匹配搜索词然后匹配任何内容(由于通配符 %)

因为你想匹配名称中间的搜索词,你还需要在开头添加通配符:

 $sql.=" WHERE employee_name LIKE '%".$requestData['search']['value']."%' ";

请注意,这将禁止在 employee_name 上使用索引,这对您来说可能是问题,也可能不是问题。

这不是最好的搜索方法,您不应该检查所有三个字段,而是询问搜索者使用哪个。毕竟年龄和薪水可以有一些匹配的数字。

搜索 27,可能会匹配 27 岁或 27000 薪水等。而且没有人的年龄bob,因此进行该搜索毫无意义

关于javascript - 数据表+服务器端处理+搜索过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com