Welcome Guest, Not a member yet? Register   Sign In
extending a simple search
#1

[eluser]snowstar[/eluser]
Hi All,

I've been using a simple search on my site for awhile now how-ever i would like to extend it to help limit some of the search results i get back from my queries.

I search via first name, lastname or address to get my results currently. My DB has now gotten much larger so i would like to implement multiple word search so i can search by first name, lastname and address for a more define search.

My current controller

Code:
class Search extends Controller {
        
    function index()
    {
        
        $data = array();
        $this->load->model('search_model');
        
        $search_data = $this->input->post('search');
        
        if($query = $this->search_model->getSearchResults($search_data))
        {
            $data['records'] = $query;
        }
        
        $this->load->view('includes/header.php');
        $this->load->view('search_results', $data);
        $this->load->view('includes/footer.php');
        
    }
}

my current model

Code:
class Search_model extends Model {

    function Search_model()
    {
        parent::Model();
    }

    function getSearchResults ($searchdata)
    {
        $this->db->like('firstname', $searchdata);
        $this->db->or_like('lastname', $searchdata);
        $this->db->or_like('addressA1', $searchdata);
        $this->db->or_like('addressA2', $searchdata);
        $this->db->or_like('addressB1', $searchdata);
        $this->db->or_like('addressB2', $searchdata);
        $this->db->orderby('firstname');
        $query = $this->db->get('clients');
        return $query->result();
    }

}

I've searched around and found a post which best matches what i want http://ellislab.com/forums/viewthread/113868/

how-ever he doesn't actually post how he sorted it. Any help with pointing me in the right direction is much appreciated.

Thanks
#2

[eluser]SPeed_FANat1c[/eluser]
You mean you want lets say result to macth first_name AND last_name? If first_name matches but last_name does't then not show in results?

I think then you should do qyuery like this:

Code:
$this->db->like('firstname', $searchdata);
$this->db->like('lastname', $searchdata);
$query = $this->db->get('clients');

I replaced or_like with like. It now should generate query with AND, so it will return when both firstname and lastname match.
Did not test it.
#3

[eluser]snowstar[/eluser]
Thanks for the reply,

no i mean i want to search for first name, last name and address to get a result. sort of like "First Name Last Name 123 Street " as the search query.

sorry i didnt make that one clear.

I tried what you suggested but had no luck
#4

[eluser]SPeed_FANat1c[/eluser]
oh, my example is not that good because it uses the same $searchdata for forst name and last name. You want to enter all search data in one field?

The more simple way would be to create several input fields - one for first name, another for last name, etc. And then do like this:

Code:
$this->db->like('firstname', $input_firstname);
$this->db->like('lastname', $input_lasstname);
$query = $this->db->get('clients');




Theme © iAndrew 2016 - Forum software by © MyBB