PHP mysql多字搜索查询

时间:2015-08-16 08:23:17

标签: php mysql codeigniter

我查询了搜索(最多三个单词),但在增加单词数时它没有多大用处,因此我需要一个代码来代替n个代码

这里是my site

 public function search() {
            $s_keywords = $this->input->get('q');           
            $arr = explode(" ", $s_keywords);

            if (count($arr) > 0) {
                $query = $this->db->query("SELECT 
                    vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name 
                    FROM tbl_vacancy
                    WHERE CONCAT(vc_title, ' ', tbl_company_cmp_name)  =  '$s_keywords' ");             
                $result = $query->result();

                return array( 'job_search' => $result,);
            }

            if (count($arr) == 1) {
                $query = $this->db->query("SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy
                WHERE vc_title LIKE '%{$s_keywords}%' OR tbl_company_cmp_name LIKE '%{$s_keywords}%' ");
                $result = $query->result();

                return array( 'job_search' => $result,);
            } else if (count($arr) == 2) {
                $query = $this->db->query("SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy
                WHERE (vc_title LIKE '%{$arr[0]}%' OR tbl_company_cmp_name LIKE '%{$arr[0]}%' ) "
                . "AND (vc_title LIKE '%{$arr[1]}%' OR tbl_company_cmp_name LIKE '%{$arr[1]}%') ");
                $result = $query->result();

                return array( 'job_search' => $result,);
            } else if (count($arr) == 3) {
                $query = $this->db->query("SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy
                WHERE (vc_title LIKE '%{$arr[0]}%' OR tbl_company_cmp_name LIKE '%{$arr[0]}%' ) "
                . "AND (vc_title LIKE '%{$arr[1]}%' OR tbl_company_cmp_name LIKE '%{$arr[1]}%') "
                . "AND (vc_title LIKE '%{$arr[2]}%' OR tbl_company_cmp_name LIKE '%{$arr[2]}%') ");
                $result = $query->result();

                return array( 'job_search' => $result,);
            }
        }

0 个答案:

没有答案