Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不同的mysql查询取决于复选框_Php_Mysql_Codeigniter - Fatal编程技术网

Php 不同的mysql查询取决于复选框

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

尝试创建搜索功能,取决于它可以找到文章标题、文本和标签的复选框

这是我的函数,但它对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 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;
}