这是我使用的代码,它只搜索(从第一个字母到最后一个字母开始),而不是逐字搜索。怎么可能一个字一个字地写出来?
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sample";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn,"utf8");
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0=> 'app_id',
1 =>'fullname',
);
// getting total number records without any search
$sql = "SELECT app_id";
$sql.=" FROM applicants";
$query=mysqli_query($conn, $sql) or die("employee-grid-data1.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT app_id, fullname";
$sql.=" FROM applicants WHERE 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( app_id LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR fullname LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR contact LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR address LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR photo LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR datereg LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("employee-grid-data1.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.
$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 */
$query=mysqli_query($conn, $sql) or die("employee-grid-data1.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["app_id"];
$nestedData[] = $row["fullname"];
$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
?>
问题是:它是从第一个字母开始到最后一个字母,而不是逐字逐句。有可能一个字一个字地翻译吗?
源您可以使用 REGEXP
和[[:<:]]
和[[:>:]]
字边界标记只匹配单词。
SELECT *
FROM table
WHERE keywords REGEXP '[[:<:]]word[[:>:]]'
还需要用mysqli_real_escape_string()
转义数据。
见下面更新的代码:
// If there is a search parameter
if( !empty($requestData['search']['value']) ) {
$search = mysqli_real_escape_string(
$conn,
// Match beginning of word boundary
"[[:<:]]".
// Replace space characters with regular expression
// to match one or more space characters in the target field
implode("[[.space.]]+",
preg_split("/s+/",
// Quote regular expression characters
preg_quote(trim($requestData['search']['value']))
)
).
// Match end of word boundary
"[[:>:]]"
);
$sql.=" AND ( app_id REGEXP '$search' ";
$sql.=" OR fullname REGEXP '$search' ";
$sql.=" OR contact REGEXP '$search' ";
$sql.=" OR address REGEXP '$search' ";
$sql.=" OR photo REGEXP '$search' ";
$sql.=" OR datereg REGEXP '$search' )";
}
作为另一种选择,您可以考虑使用全文搜索。
如果我理解正确,您指的是搜索由搜索框中的keyup事件触发的事实。这个javascript将允许用户输入一个单词,然后按回车键来执行搜索。
这需要添加到包含数据表初始化代码的同一个js文件中,并且在初始化代码之后:
var oTable = $('#example').dataTable({
... yourdatatable init code
// unbind the keyup event that triggers the search
$("#example_filter input").unbind();
// use fnFilter() to perform the search when the `Return` key is pressed
$("#example_filter input").keyup(function (e) {
if (e.keyCode === 13) {
oTable.fnFilter(this.value);
}
});
这里假设数据表是v1.9。如果你使用的是1.10,这里有一个SO答案,它概述了修改
工作版本-> https://jsfiddle.net/markps/HEDvf/3225/
这是Gyrocode.com提供的解决方案
// If there is a search parameter
if( !empty($requestData['search']['value']) ) {
$search = mysqli_real_escape_string(
$conn,
// Match beginning of word boundary
"[[:<:]]".
// Replace space characters with regular expression
// to match one or more space characters in the target field
implode("[[.space.]]+",
preg_split("/s+/",
// Quote regular expression characters
preg_quote(trim($requestData['search']['value']))
)
).
// Match end of word boundary
"[[:>:]]"
);
$sql.=" AND ( app_id REGEXP '$search' ";
$sql.=" OR fullname REGEXP '$search' ";
$sql.=" OR contact REGEXP '$search' ";
$sql.=" OR address REGEXP '$search' ";
$sql.=" OR photo REGEXP '$search' ";
$sql.=" OR datereg REGEXP '$search' )";
}