Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Foreach loop runs OK on localhost, but fails online
#1

[eluser]Arministrator[/eluser]
First off, Hi great Codeigniter community. I've been present as a guest on forums for a while now, but this is my first post.

I've browsed and searched and googled first, for a solution of what should be a tiny problem for you guys, but in the end I had to start this topic.

I'm using a really simple getAll function in my model to retrieve data. That function is called from a controller and passes the data as an array to the view. And it all works fine on localhost (WAMP), but fails online. I'm baffled.

The Model:

Code:
<?php

class Data_model extends CI_Model {
    
        function getAll() {
            
            $q = $this->db->get('data');
                if($q->num_rows() > 0) {
                foreach ($q->result() as $row) {
                    
                    $data[] = $row;
                }
            return $data;
            }
        }
    }

?>

The Controller:

Code:
<?php

class Site extends CI_Controller {
    
    function index() {
    
        $title['title'] = "Site title";
        $this->load->model('data_model');
        $data['rows'] = $this->data_model->getAll();
        $this->load->view('header', $title);
        $this->load->view('content', $data);
        $this->load->view('footer');
        }
        
    function Project() {
        
        $this->load->view('project');
        }
    
    
    }
?>

The View:

Code:
<?php foreach($rows as $r):?>
<h1>&lt;?php echo $r->title;?&gt;</h1>
<p>&lt;?php echo $r->content;?&gt;</p>
<p>&lt;?php echo $r->author;?&gt;</p>
&lt;?php endforeach; ?&gt;


All of this stuff is more or less from the Net Tuts CI series. I've seen this problem posted in comments, but no solution. And this is really a tripping point for me, because how do I know that I'm doing anything right (or wrong) if it works locally and fails online.

Again, sorry if this has been discussed before.

EDIT: Oh, this is the error I get:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/content.php

Line Number: 2
#2

[eluser]Arministrator[/eluser]
Aren't I stupid: My online database was empty.




Theme © iAndrew 2016 - Forum software by © MyBB