anulogi anulogi - 5 months ago 19
MySQL Question

Display 2 tables on codeigniter

im new to codeigniter and php, need some enlightment to display 2 tables with mvc method. just displaying two tables in one page (camera and visitor table). here is my code

Model :

function m_report() {
$camera = $this->db->get('camera');
return $camera->result();
$report = $this->db->get('visitor');
return $report->result();
}


View:

<?php foreach($data1 as $report){ ?>
<tr>
<td><?php echo $report->Country; ?></td>
<td><?php echo $report->Days; ?></td>
</tr>
<?php } ?>

<?php foreach($data_camera as $camera){ ?>
<tr>
<td><?php echo $camera->cameratype; ?></td>
</tr>
<?php } ?>


Controller :

function report(){
$data['data_camera']=$this->m_data->m_report();
$data1['data1']=$this->m_data->m_report();
$this->load->view('v_report',$data,$data1);


}


the problem is, i can display camera table but visitor got error Message: Undefined variable: data1

Can anyone help me to figure it out? Much appreciate

Answer

You can only return ONE thing from a method - once you return something, execution of code stops.

function m_report() {

    $camera = $this->db->get('camera')->result();
    $report = $this->db->get('visitor')->result();

    return array_merge($camera, $report);
}

Now you get an array with all the results from both "camera" and "visitor". You can specify it out if you'd like with an associative array.

function m_report() {

    $data['camera'] = $this->db->get('camera')->result();
    $data['visitor'] = $this->db->get('visitor')->result();

    return $data;
}