Problems in a Select CodeIgniter + PHP

1

Hello, I have the following case, I have a donor who wants to make a donation at a hemocenter, he does the search for this hemocenter and clicks a button I want to donate here. This will create a table in the database called donation_marked, with data: donor_id, middle_id, shift, date_possible_date . Now when I log into the hemocenter on the home screen should appear the data from the donor table that is equal to the donor id of the donation_marked table, in addition to the other data in the donation table marked. But only the donor data is showing up. Follow my select.

    $status = 'Aguardando Confirmação';
    $this->db
    ->select("*")
    ->from("doacao_marcada")
    ->where('status_doacao_marcada', $status)
    ->where('id_hemocentro', $this->session->userdata('id_hemocentro'));
    $query = $this->db->get()->result();
    foreach ($query as $row) {
        $this->db->select("*")
        ->from("doador")
        ->where("id_doador", $row->id_doador);
        return $this->db->get()->result();
    }
    
asked by anonymous 03.11.2016 / 11:50

1 answer

1

If the relationship already exists between the tables, you do not have to do one query at a time, you should make a join in the query itself, which in this case should look like this:

$status = 'Aguardando Confirmação';
$this->db
->select("*")
->from("doacao_marcada")
->join("doador", "doacao_marcada.id_doador = doador.id")
->where('status_doacao_marcada', $status)
->where('id_hemocentro', $this->session->userdata('id_hemocentro'));
return $this->db->get()->result();
    
03.11.2016 / 12:01