Search data from 2 tables - PHP+Codeigniter.

Asked

Viewed 637 times

0

Hello, I’m starting my studies in codeIgniter and I came across the following.

I have an employer table and another of vacancies, the job has employer id. I would like to show the data of the 2 tables. For example: I type something and it will appear the vacancy and employer data. To appear the vacancy data I did:

Model:

function getVaga($campo_busca) {

$this->db
          ->select("*")
          ->from("dados_vaga")
          ->like('cargo',$campo_busca); 


    return  $qr = $this->db->get()->result();

}   

Already on the controller:

  public function teste(){

  $campo_busca = $this->input->post('teste');

  $data = array(
      "dados" => $this->Vaga_model->getVaga($campo_busca)
  );


  $this->load->view('busca', $data);

The employer id is in this table data_vacancy to know which is the owner of this vacancy, now I do not know how to pass the same as parameter to another query.

Good evening to all, thank you to whom to reply, big hug.

1 answer

1


Try this:

function getVaga($campo_busca) {

     $this->db
          ->select("*")
          ->from("dados_vaga")
          ->join("dados_empregador", "dados_empregador.id = dados_vaga.id_empregador")
          ->like('dados_vaga.cargo',$campo_busca); 

    return  $qr = $this->db->get()->result();

}   

Obviously replace "employer data" and ID fields between the 2 tables as needed.

More information about JOIN in CI can be found in the link below: https://www.codeigniter.com/userguide3/database/query_builder.html

Good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.