• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching Query

#1
I am creating a search query. When I submit my form and if I search for a category lets say codeigniter then should get all questions within that category or if I search for a question it will just show questions that match.


I can only search for title at the moment in questions.

Is there any better way?


PHP Code:
// $this->db->or_where('c.name', $search);

public function search_questions($search) {
    $this->db->select('*');
    $this->db->from('question q');
    $this->db->join('category c''c.category_id = q.category_id');
    $this->db->like('q.title'$search);
    $this->db->or_like('c.name'$search);
    $query $this->db->get();
    return $query->result_array();





PHP Code:
<?php 

class Search extends MY_Controller {

    public function 
__construct() {
        
parent::__construct();
        
$this->load->model('answers/answer_model');
    }

    public function 
index() {
        if (
$this->input->post('search')) {

            
$results $this->search_questions($this->input->post('search'));

            
$data['questions'] = array();

            if (
$results) {

                foreach (
$results as $result) {
                    
$data['questions'][] = array(
                        
'question_id' => $result['question_id'],
                        
'title' => $result['title'],
                        
'votes' => $result['votes'],
                        
'answers' => $this->answer_model->count_answers($result['question_id']),
                        
'views' => ''
                    
); 
                }

            }

            
$data['page'] = 'search/search_results';

            
$this->load->view($this->config->item('config_template') . '/template/template_view'$data);

        }
    }

    public function 
search_questions($search) {
        
$this->db->select('*');
        
$this->db->from('question q');
        
$this->db->join('category c''c.category_id = q.category_id');
        
$this->db->like('q.title'$search);
        
$this->db->or_where('c.name'$search);
        
$query $this->db->get();
        return 
$query->result_array();
    }



Attached Files Thumbnail(s)
   
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.