2012-04-21 4 views
0

고급 검색 codeignitor이 문제를 해결하지만 난 모델에 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 입력 텍스트 네 fildes가 섹션에 대한 발행자 3 선택 상자 저자 -3- 선택 상자 2- 선택 상자 이름

모델

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 .= ';'; 


    } 


} 
이다,174,515

이나중에 사용하기 위해 쿼리 문자열을 저장하지 않습니다 컨트롤러

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 */ 

답변

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); 
+0

오류 줄은이 줄을 가리 킵니다. $ sql. = "WHERE". implode ("AND", $ searches); – user1080247

+0

@ user1080247 예, 알고 있습니다. 내 설명을 읽었 니? –

+0

네 카말 .. 네가 맞지만 뭔가 잘못 됐어. – user1080247