Problems with a Select Codeigniter + PHP

Asked

Viewed 164 times

1

Hello, I have the following case, I have a donor who wishes to make a donation in a center Hemocentro, it makes the search of this center and click on a wish button to donate here. This will create a table in the database called donation marked, with the data: id_donor, id_hemocenter, shift, data_possible_doacao . Now when I log in to Hemocentro on the home screen should appear table data donor that was the same as the donor id_of the marked donor table, in addition to the other data of the marked donation table. But only the donor data is appearing. 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();
    }
  • when you give a return, the function simply returns what is going on there and for the execution of the method, what is unclear is exactly what information you need. You want to return the data of ALL donors, right?

1 answer

1

If the relationship already exists between the tables, you do not need to make one query at a time, you should make one join in the consultation itself, which in this case should remain so:

$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();

Browser other questions tagged

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