Php 不同的mysql查询取决于复选框
尝试创建搜索功能,取决于它可以找到文章标题、文本和标签的复选框 这是我的函数,但它对DB使用了多个请求,我正在尝试发出w/一个请求;) 我在DB中有3个表:articles-id,text;标签-id、名称;articles\u tag\u rel-article\u id,tag\u id 如何更好地检查用户单击的复选框,以及如何构建查询以获得带有1个请求的文章 泰 UPD:我自己做的:)这是新版本:Php 不同的mysql查询取决于复选框,php,mysql,codeigniter,Php,Mysql,Codeigniter,尝试创建搜索功能,取决于它可以找到文章标题、文本和标签的复选框 这是我的函数,但它对DB使用了多个请求,我正在尝试发出w/一个请求;) 我在DB中有3个表:articles-id,text;标签-id、名称;articles\u tag\u rel-article\u id,tag\u id 如何更好地检查用户单击的复选框,以及如何构建查询以获得带有1个请求的文章 泰 UPD:我自己做的:)这是新版本: /** * Search articles by text/title/tags depe
/**
* Search articles by text/title/tags depends on checkboxes values
* @param array $data of checkboxes values, keys can be 'tag'/'title'/'text'
* @return array $articles of non-repeat articles
*/
public function search_articles($data)
{
$str = "";
if (isset($data['tag'])) {
$this->db
->select('articles.*')
->from('articles')
->from('tags')
->from('articles_tags_rel')
->where('articles.id = articles_tags_rel. article_id')
->where('articles_tags_rel.tag_id = tags.id')
->where('tags.name', $data['tag']);
$str = $this->db->get_compiled_select();
unset($data['tag']);
}
foreach ($data as $key => $value) {
$this->db
->select('*')->from('articles')
->like($key, $value);
$str .= (!$str) ? $this->db->get_compiled_select() : " UNION ". $this->db->get_compiled_select();
}
$articles = $this->db->query($str)->result_array();
return $articles;
}
/**
* Search articles by text/title/tags depends on checkboxes values
* @param array $data of checkboxes values, keys can be 'tag'/'title'/'text'
* @return array $articles of non-repeat articles
*/
public function search_articles($data)
{
$str = "";
if (isset($data['tag'])) {
$this->db
->select('articles.*')
->from('articles')
->from('tags')
->from('articles_tags_rel')
->where('articles.id = articles_tags_rel. article_id')
->where('articles_tags_rel.tag_id = tags.id')
->where('tags.name', $data['tag']);
$str = $this->db->get_compiled_select();
unset($data['tag']);
}
foreach ($data as $key => $value) {
$this->db
->select('*')->from('articles')
->like($key, $value);
$str .= (!$str) ? $this->db->get_compiled_select() : " UNION ". $this->db->get_compiled_select();
}
$articles = $this->db->query($str)->result_array();
return $articles;
}