简体   繁体   中英

error in join with codeigniter: variable query

I have two tables patient (name, id_paz) and visit (id_visita, id_paz, date), 1 patient may have more visits. Taking as below, I check the error: "Undefined variable: query" and "Invalid argument supplied for foreach ()"

Model:

function result_getall(){

    $this->db->select('*');
    $this->db->from('paziente');
    $this->db->join('visita', 'paziente.id_paz = visita.id_visita', 'left'); 
    $query = $this->db->get();
    return $query->result($sql);

    }

View:

<?php foreach ($query as $row): ?>      

                   <?php echo $row->id_paz?><br>
               <?php echo $row->nome?><br>
               <?php echo $row->cognome?><br>


                    <?php echo $row->id_visita ?>
         <?php endforeach; ?>

Controller:

function getall(){      
    $this->load->model('visita_model');
    $data['query'] =$this->visita_model->result_getall();
    print_r($data['query']);
    die();
    $this->load->view('aggiungi_view', $data);
    }

Maybe your array isn't set.
try to check it with print_r or adding an if statement like this:

<?php 
    if(isset($query)){
        foreach ($query as $row): ?>      

                   <?php echo $row->id_paz?><br>
               <?php echo $row->nome?><br>
               <?php echo $row->cognome?><br>


                    <?php echo $row->id_visita ?>
         <?php endforeach; 
     }
     ?>

and in your controller change this:

return $query->result($sql);

to this:

$return = array();
foreach ($query->result() as $row)
    array_push($return, $row);

return $return; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM