Welcome Guest, Not a member yet? Register   Sign In
Free Noobcake for Everyone
#1

[eluser]Bionicjoe[/eluser]
Sorry if I am yet another clueless noob posting the same question in the wrong forum, but I'm stumped.

I've been following the CI tuts on Tuts Plus to learn CI. I've taken a past lesson and swapped in my own code and now I'm stuck with:
Quote:Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/outageview.php

Here's the code
Controller
Code:
<?php

class Outage extends Controller {
    function index() {        
        $this->load->model('outage_model');
        $data['row'] = $this->outage_model->getAll();
        
        $this->load->view('outageview', $data);
    }
}

?>

Model
Code:
<?php

class Outage_model extends Model {
    
function getAll() {
        $this->db->select('ticketnumber');
        $this->db->from('outage');
        $this->db->where('ticketnumber', 'NOT NULL');
        
        $q = $this->db->get();
        
        if($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }
}

View
Code:
<html>
<head>
<title><?php echo 'title'?></title>
</head>
<body>
<h1>&lt;?php echo 'heading'?&gt;</h1>
    
<h3>Outages</h3>    

<ol>
&lt;?php foreach($row as $item):?&gt;

<li>&lt;?php echo $item;?&gt;</li>

&lt;?php endforeach;?&gt;
</ol>
    
&lt;/body&gt;
&lt;/html&gt;

This is really similar to one of the lessons, and I've boiled down the query to be as simple as possible. Know I'm missing something obvious.
#2

[eluser]tomcode[/eluser]
function getAll() does not always return a result, in these cases the foreach loop in the view will cause an error.
#3

[eluser]Bionicjoe[/eluser]
Thanks tomcode, but I'm certain this query is returning a result.
I should have posted that originally.
#4

[eluser]tomcode[/eluser]
Do a print_r($row) in the view, then You're fixed.
#5

[eluser]Bionicjoe[/eluser]
Thanks.
Got me headed in the right direction!
#6

[eluser]Bionicjoe[/eluser]
Solved it now!!

In my model I was returning a result(), but I needed result_array().

My query was working but the result() wouldn't accept the data. So the query would return empty, and thus the problem I was having.

Now I just need to get my values out of the array.

Thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB