[英]Search using keywords in multiple tables?
I am trying to set up my search bar to create a table of results when searching through two tables. 我试图设置我的搜索栏,以便在搜索两个表时创建一个结果表。
Each table (fresh, salt) has 2 columns (ID and DESCRIPTION). 每个表(新鲜,盐)都有2列(ID和DESCRIPTION)。
if(isset($_REQUEST["term"])){
// Prepare a select statement
$term = $_REQUEST['term'];
$sql = "SELECT * FROM fresh WHERE
description LIKE '%{$term}%' or
sku like '%{$term}%'
union
SELECT * FROM marine WHERE
description LIKE '%{$term}%' or
sku like '%{$term}%'";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_term);
// Set parameters
$param_term = $_REQUEST["term"] . '%';
Searched terms can be anything like, "Neon Tetra", but the search currently looks for that string as is and I'd like for it to search as two+ different keywords separated by space, even if terms are backward such as "Tetra Neon". 搜索的词可以是“ Neon Tetra”之类的东西,但是搜索当前会按原样查找该字符串,并且我希望它以两个+个不同的关键字(以空格分隔)进行搜索,即使术语倒退,例如“ Tetra Neon” 。
which id relatating with both table use that.im just giving example.don't know which table's have primary key or secondary. 与这两个表相关的哪个ID使用that.im仅给出example。不知道哪个表具有主键或辅助键。 try this: 尝试这个:
$this->db->join('fresh', 'fresh.id = salt.freshid')
->select('fresh.*,salt.id');
//you can all fields from salt table using salt.id,salt.name etc. //您可以使用salt.id,salt.name等从salt表中的所有字段
$where = '';
if ($this->input->post('search')['value'] && $this->input->post('search')['value'] != '') {
$where = "(fresh.sku LIKE '%" . $this->input->post('search')['value'] . "%' OR saly.sku LIKE '%" . $this->input->post('search')['value']. "%')";
}
if ($where != '') {
$this->db->where($where);
}
$count = $this->db->get('salt')->num_rows();
if ($this->input->post('length') != '-1') {
$this->db->limit($this->input->post('length'), $this->input->post('start'));
}
$query = $this->db->get('salt')->result_array();
return [$query, $count];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.