0

So i have a searchbox to search in my factories table. it works like a charm, but i'm still new to codeigniter.

Do i need to use a select to select more than one table in my database?

My controller function:

    function search()
    {
        $this->load->view('views/header');
        $this->load->view('views/search');
        $this->load->view('views/footer');
    }

    function searchresults()
    {
        $match = $this->input->post('search');
        $data['query'] = $this->bedrijven_model->get_search($match);
        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
    }

My model:

function get_search($match)
{
    $this->db->like('Bedrijfsnaam', $match);
    $this->db->or_like('Postcode', $match);
    $this->db->or_like('Plaats', $match);
    $this->db->or_like('Telefoonnummer', $match);
    $this->db->or_like('Email', $match);
    $this->db->or_like('Website', $match);
    $this->db->or_like('Profiel', $match);
    $this->db->or_like('Adres', $match);
    $query = $this->db->get('bedrijven');

    return $query->result();
}

I'm also wondering if you can put your searchfunction from the model into the controller. because then i need to make a new model function for each table.

Thanks in advance.

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106

2 Answers2

4

Try to join two tables and then apply like

$this->db->like('Bedrijfsnaam', $match);
$this->db->or_like('Postcode', $match);
$this->db->or_like('Plaats', $match);
$this->db->or_like('Telefoonnummer', $match);
$this->db->or_like('Email', $match);
$this->db->or_like('Website', $match);
$this->db->or_like('Profiel', $match);
$this->db->or_like('Adres', $match);

Search for match in table2 in col1,col2

$this->db->or_like('table2.col1',$match);
$this->db->or_like('table2.col2',$match);

Finally join the table to which you have applied likes above with the table1

$this->db->join('table2','table1.id = table2.id');
$query = $this->db->get('bedrijven');
GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • but it does not quite work. when i apply a join the searchresults are empty – Kees Sonnema Apr 02 '13 at 12:13
  • Then check it in DB...may be there is no records with the combination of match and join – GautamD31 Apr 02 '13 at 12:16
  • hm the it's a bit confusing. it only shows records who have an id in my second table and in my first table. so when i have a table record with id 1 then it does not show when it hasn't an id 1 in the second table. – Kees Sonnema Apr 02 '13 at 12:21
  • So......if you want them individually then No way....You need to gothrough individually... – GautamD31 Apr 02 '13 at 12:25
  • i have a table tags. here i stored tags for my factories. but how more tags i add how often the factory is shown at the searchresults – Kees Sonnema Apr 02 '13 at 12:28
  • Hey am I rude...?No iam just telling you that's it...Iam not such a guy by the way – GautamD31 Apr 02 '13 at 12:31
  • i think i have to ask a new question for that then. because it's stupid that it shows me the records more than once. – Kees Sonnema Apr 02 '13 at 12:33
  • that are the rules. you don't just ask two question in one. if you understand what i mean. i'm not scolding about myself. – Kees Sonnema Apr 02 '13 at 12:37
0

The example in the User Guide should explain this:

$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id

See the whole thing under Active Record page in the User Guide.

Goran
  • 3,292
  • 2
  • 29
  • 32