nadine deskananda
nadine deskananda

Reputation: 3

Message: Invalid argument supplied for foreach() Codeigniter

I got an error message: Invalid argument for foreach() in my View. I wanted to display all data of my mysql table but I kept on getting error message. I am a newbie in Codeigniter, I have no clue how to solve this. Here are my code.

code in view (views/dashboardutama.php)

<table class="table table-striped projects" id="example1" border="1"> 
    <thead>
        <th style="padding:10px; text-align: center;" rowspan="2">STO</th>
        <th style="padding:10px; text-align: center;" colspan="3">Sales Non valid</th>
        <th style="padding:10px; text-align: center;" colspan="3">Sales HI</th>
    </thead>
    <tbody>
        <?php
        $i=1;
        foreach ($dashboardutama as $row) {
            ?>
            <tr>
                <td><?php echo $i; ?></td>
                <td><?php echo $row->$sto; ?></td>
                <td><?php echo $row->$salesnonv; ?></td>
                <td><?php echo $row->$saleshi; ?></td>
            </tr>
            <?php
            $i=$i+1;
        }
        ?>
    </tbody>
</table>

my controller (controller/dashboardutama.php)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboardutama extends CI_Controller {

public function index()
{
    $this->load->model('model_dashu');
    $data['dashboardutama'] = $this->model_dashu->get();
    $this->load->view('template/header');
    $this->load->view('dashboardutama', $data);
    $this->load->view('template/footer');
}


My model (models/model_dashu.php)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_dashu extends CI_Model {

public function get(){
    $this->db->get('dashboardutama')->result();
}

I'm a beginner so it is so hard for me to solve. Thank you for your helps.

Upvotes: 0

Views: 37

Answers (1)

Naveen Kumar
Naveen Kumar

Reputation: 160

You forgot to return the result in the get method. Should have been:

public function get(){
    return $this->db->get('dashboardutama')->result();
}

Upvotes: 0

Related Questions