anulogi
anulogi

Reputation: 13

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

Upvotes: 1

Views: 1416

Answers (2)

abanoub metyas
abanoub metyas

Reputation: 66

you can not make 2 returns in one method

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

i think it is better to make each query in single function

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

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

then call them separately in controller

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

}

Upvotes: 0

CmdrSharp
CmdrSharp

Reputation: 1070

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;
}

Upvotes: 1

Related Questions