anulogi anulogi - 11 months ago 51
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();


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

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

Controller :

function report(){


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


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;