这里的代码我只使用它来搜索(从第一个字母到最后一个字母开头),而不是一个字一个字。如何逐字(关键字)制作它?
<?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
?>
问题所在:它开始写第一封信,而不是WORD的WORD。是否有可能逐字逐句地形成它?
答案 0 :(得分:2)
您可以使用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' )";
}
作为替代方案,您可以考虑使用full-text search。
答案 1 :(得分:0)
如果我理解正确 - 你指的是搜索是由搜索框中的keyup事件触发的。此javascript将允许用户输入单词,然后按Enter键执行搜索。
这需要添加到包含数据表初始化代码的同一个js文件中,并且在init代码之后:
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答案here会概述修改
工作版 - &gt; https://jsfiddle.net/markps/HEDvf/3225/
答案 2 :(得分:0)
这是由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' )";
}