0

我想构建高级搜索脚本,但模型中的 search_all 函数出现此错误

A PHP Error was encountered

Severity: 4096

Message: Object of class CI_DB_mysql_result could not be converted to string

Filename: models/search_model.php

Line Number: 129

我有四个字段 1- 输入要写书名的文本 2- 为作者选择框 3- 为出版商选择框 3- 为章节选择框

模型是

class search_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    /* This function get all search in database sort by order asc.*/

    function get_new_one($id)
    {

    $this->db->where('ne_id',$id);

    $result=$this->db->get('d_search');

    return $result->row();

    }
    //////////////frontend//////////////////////////////////////////////////////////

    function show_new($id)
    {

    $result=$this->db->query("SELECT * , COUNT( d_comments_search.cn_new_id ) as count

                        FROM d_search

                        left JOIN d_comments_search ON d_search.ne_id = d_comments_search.cn_new_id and d_search.ne_hide='1'

                        inner join d_search_category on d_search_category.nc_id = d_search.ne_category_id and d_search_category.nc_hide= '1'

                        and d_search.ne_id= $id group by d_search.ne_id");

    return $result->row() ;

    }

    function generate_results($keyword,$row=0){
        $result1 = $this->db->query("SELECT bo_id,bo_name,bo_state,bo_about FROM d_book where (bo_name like '%$keyword%' or bo_about like '%$keyword%') and bo_state = '1' limit $row,20");
        $result2 = $this->db->query("SELECT au_id,au_name,au_state,au_info FROM d_author where (au_name like '%$keyword%' or au_info like '%$keyword%') and au_state = '1' limit $row,20");
        $result3 = $this->db->query("SELECT pub_id,pub_name,pub_state,pub_info FROM d_publishing where (pub_name like '%$keyword%' or pub_info like '%$keyword%') and pub_state = '1' limit $row,20");
        $results = array_merge($result1->result_array(),$result2->result_array(),$result3->result_array());

        return $results;

    }

    // get total number of users
    function getNumUsers($keyword)
    {
        $result1 = $this->db->query("SELECT bo_id,bo_name,bo_state,bo_about FROM d_book where (bo_name like '%$keyword%' or bo_about like '%$keyword%') and bo_state = '1'");
        $result1 = $result1->num_rows();
        $result2 = $this->db->query("SELECT au_id,au_name,au_state,au_info FROM d_author where (au_name like '%$keyword%' or au_info like '%$keyword%') and au_state = '1'");
        $result2 = $result2->num_rows();
        $result3 = $this->db->query("SELECT pub_id,pub_name,pub_state,pub_info FROM d_publishing where (pub_name like '%$keyword%' or pub_info like '%$keyword%') and pub_state = '1'");
        $result3 = $result3->num_rows();
        return $result1 + $result2 + $result3;
    }
    //////////////////////////////////end paging///////////////////

    function get_publishing()
    {
        $this->db->where('pub_state','1');
        $result=$this->db->get('d_publishing')->result_array();
        return $result;
    }

    function get_author()
    {
        $this->db->where('au_state','1');
        $result=$this->db->get('d_author')->result_array();
        return $result;
    }

    function get_section()
    {
        $this->db->where('sec_state','1');
        $result=$this->db->get('d_section')->result_array();
        return $result;
    }

    function search_name()
    {
        $bo_name=$_POST['bo_name'];
        $this->db->order_by("bo_ord","asc");
        $this->db->like('bo_name',$bo_name);
        return $this->db->get('d_book')->result_array();

    }


        function search_all() {
        $publish = $this->input->post('publish');
        $author = $this->input->post('author');
        $sec_name = $this->input->post('section');

        $sql = $this->db->query("SELECT * FROM `d_book`");
        $searches = array();
        if ($publish != 'choose')
            $searches[] = "`bo_pub_id` = '$publish'";
        if ($author != 'choose')
            $searches[] = "`bo_au_id` = '$author'";
        if ($sec_name != 'choose')
            $searches[] = "`bo_sec_id` = '$sec_name'";
        if (count($searches) > 0) {
       $sql .= "WHERE " . implode(" AND ", $searches);
        }
        $sql .= ';';


    }


}

这是控制器

class Search extends front_end {
    var $temp;

    function __construct(){
       parent::__construct();
       $this->load->library('form_validation');
       //echo $this->input->post("keyboard");

    }

    public  function index()
    {
        $this->overview();
    }

    /**
     * This function display all  search
     * @param integer $row
     */
    public  function overview($row=0)
    {
        $this->form_validation->set_rules('keyword', 'كلمة البحث', 'trim|required|xss_clean|htmlspecialchars');
        $this->form_validation->run();
        $this->store_keyword();
        //echo $this->session->flashdata('keyword');
        if ($this->session->flashdata('keyword') != ''){
            $keyword=$this->session->flashdata('keyword');
            $this->generate_results($this->session->flashdata('keyword'),$row);
        }else{
        $this->generate_results($this->input->post("keyword"),$row);
        }
        //$this->session->set_flashdata('keyword', $this->input->post("keyword"));
        $data = $this->temp;

        //$this->session->keep_flashdata('keyword');
        $this->view('search/site/results', $data);
    }

    /**
     * This function generate result of search
     * @param string $keyword
     * @param integer $row
     */
    public  function generate_results($keyword,$row=0){
        $this->load->model('search/search_model','search');
        $this->load->library('pagination');
        $config['base_url'] = base_url().'search/search/overview/';
        $config['total_rows'] = $this->search->getNumUsers($keyword);
        $config['per_page'] = '20';
        $config['uri_segment'] = '4';
        $this->pagination->initialize($config);
        $data['page'] = $row;
        $data['results'] = $this->search->generate_results($keyword,$row);
        $data['total_rows'] = $this->search->getNumUsers($keyword);
        $data['links']=$this->pagination->create_links();
        $this->temp = $data;

    }

    /**
     * This function display detail of new
     * @param integer $id
     */
    public  function show($id)
    {
        $data['new']=$this->search->show_new($id);
        $count=$data['new']->ne_count_visit;
        $this->search->add_count($count,$id);
        $data['comments']=$this->search->show_comments($id);
        $this->view('site/new', $data);
    }

    /**
     * This function store keyword to use in search 
     */
    private  function store_keyword(){
        if($this->input->post("keyword")){
           $this->session->set_userdata('keyword', $this->input->post("keyword"));
       }
    }


    public  function search_form()
    {
        $this->load->model('search/search_model','search');
        $data['publish']=$this->search->get_publishing();
        $data['author']=$this->search->get_author();
        $data['section']=$this->search->get_section();
        $this->view('search/site/adv_search_form',$data);
    }

    public function search_adv()
    {
       $this->load->model('search/search_model','search');
       $data['bo_name']=$this->input->post('bo_name');
       $data['section']=$this->input->post('section');
       $data['publish']=$this->input->post('publish');
       $data['author']=$this->input->post('author');
       $data['result1'] = '';
       $data['result2'] = '';
       $data['result3'] = '';
       $data['result4'] = '';
       if($data['bo_name'] == NULL and $data['section']== NULL and $data['publish']==NULL and $data['author']== NULL){
            $this->search_form();
       }else{
            if(isset($data['bo_name']) and $data['bo_name']!= NULL)
            {
               $data['result1'] = $this->search->search_name();
}
           if(isset($data['section']) and $data['section'] != NULL)
            {
               $data['result2']=$this->search->search_all();
            }
            if(isset($data['publish']) and $data['publish'] != NULL)
            {
               $data['result3']=$this->search->search_all();
            }
            if(isset($data['author']) and $data['author']!= NULL)
            {
               $data['result4']=$this->search->search_all();
            }
            $data['no_results'] = '';
            if(! $data['result1'] &&  !$data['result2'] &&  !$data['result3'] &&  !$data['result4']){
               $data['no_results'] = TRUE;
            }else{
                $data['no_results'] = FALSE;
            }
            $this->view('search/site/search_result',$data);
        }
    }
}

/* End of file dashboard.php */
4

1 回答 1

1

$this->db->query()不存储查询字符串供以后使用。它实际上立即执行查询并返回CI_DB_mysql_result. 您应该在调用之前构建查询字符串$this->db->query()并将其存储到变量中,然后将其传递给方法。

$sql = "SELECT * FROM `d_book` ";
$searches = array();
if ($publish != 'choose')
{
    $searches[] = "`bo_pub_id` = '__some_id__'";
}

if ($author != 'choose')
{
    $searches[] = "`bo_au_id` = '__some_id__'";
}

if ($sec_name != 'choose')
{
    $searches[] = "`bo_sec_id` = '__some_id__'";
}

if (count($searches) > 0) 
{
    $sql .= "WHERE " . implode(" AND ", $searches);
}

$sql .= ';';
$this->db->query($sql);
于 2012-04-21T12:19:20.327 回答