SAR
SAR

Reputation: 1845

Codeigniter search query, I want to add another condition to my search

This is my table:

Category:

{category_id , category }

Document:

{document_id, title, address, category}

My controller:

Class Search Extends CI_Contrller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mymodel');
    }

    function search_keyword()
    {
        $keyword    =   $this->input->post('keyword');
        $data['results']    =   $this->mymodel->search($keyword);
        $this->load->view('result_view',$data);
    }

}

My model:

Class Mymodel Extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function search($keyword)
    {
        $this->db->like('name',$keyword);
        $query  =   $this->db->get('document');
        return $query->result();
    }
} 

And this is my view:

<form action="<?php echo site_url('search/search_keyword');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" value = "Search" />
</form>
<table>
?>
foreach($results as $row){
<?php
    <tr>
        <td><?php echo $row->title?></td>
        <td><?php echo $row->category?></td>
    </tr>
<?php   
}
?>
</table>

According to the controller and model shown above, I can only retrieve one keyword, which is my title of the document, but now I want to:

Add to condition one if title = $keyword and category=$keyword2.

Is it possible in this MVC or not?

Upvotes: 2

Views: 8557

Answers (4)

Zeeshan
Zeeshan

Reputation: 801

Yes that is possible in Codeigniter & you should have get it working by the above solutions posted.

No problem though.Try to follow this steps & let me know if you find any problem.

1) Add input text field named 'keyword2' to your view
2) Read 'keyword2' value in Controller like 'keyword' 
**$keyword = $this->input->post('keyword');**

Pass both keywords to model

3) $data['results'] = $this->mymodel->search($keyword,$keyword2);

Your Search function in model should look like this :

function search($keyword,$keyword2)
{
    $this->db->like('name',$keyword);
    $this->db->like('name',$keyword2);
    $query  =   $this->db->get('document');
    return $query->result();
}

Hopefully this helps you.

Best Regards, Zeeshan.

Upvotes: 0

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

Simply use one extra like statement but for this you require one new argument $keyword2.pass that from controller to model.

$this->db->select('*'); 
 $this->db->from('document');
 $this->db->like('name', $keyword1);   
 $this->db->like('category', $keyword2);       
 return $this->db->get()->result();

Upvotes: 1

DAG
DAG

Reputation: 6994

Just add another method to your model where you are querieng for the keyword2 in the category field.
In your controller simply assign the return to another array item like 'results2'. Hope that this gives you an idea.

Upvotes: 0

Rajendra Yadav
Rajendra Yadav

Reputation: 635

Try it.

  Class Mymodel Extends CI_Model
 {
  function __construct()
  {
    parent::__construct();
    }

  function search($keyword)
  {
    $this->db->where('name',$keyword);
    $this->db->where('category',$keyword2);
    $query  =   $this->db->get('document');
    return $query->result();
  }

}

Or you can also use condition.

 $this->db->like('name',$keyword);
 $this->db->like('category',$keyword2);

And define $keyword2 in your controllers

Upvotes: 3

Related Questions