[eluser]hellboy[/eluser]
Hi Everyone,
Been using Ci for a couple of days now, and I'm pretty impressed so far, but seem to be stuck with a problem 'Fatal error: Call to a member function on a non-object' in one of my views. From what I can see the connection to the DB seems ok and the query is returning data.
Here's some code:
The Controller
Code:
<?php
class News extends Controller {
function News()
{
parent::Controller();
$this->load->scaffolding('articles');
}
function index()
{
// $this->load->view('newsview');
$this->load->model('Articles_model');
$data['head'] = "My Real Title";
$data['heading'] = "My Real Heading";
$data['query'] = $this->Articles_model->get_last_ten_articles();
$this->load->view('newsview', $data);
}
function comments()
{
echo 'Look at this!';
}
}
?>
The View
Code:
<html>
<head>
<title>Jagungal.info</title>
</head>
<body>
<h1>Welcome to Jagungal</h1>
<h2><?php echo $heading; ?></h2>
<p><?php echo $head; ?></p>
<hr />
<?php foreach($query->result() as $row): ?>
<h3><?=$row->title?></h3>
<p><?=$row->summary?></p>
<?php endforeach;?>
</body>
</html>
The Model
Code:
<?php
class Articles_model extends Model {
function Articles_model()
{
parent::Model();
}
function get_last_ten_articles()
{
$this->load->database();
//$query = $this->db->get('articles');
$query = $this->db->query('SELECT * FROM articles');
if($query->num_rows() > 0){
echo $query->num_rows().' Rows';
return $query->result();
} else {
echo 'No Rows';
return array();
}
}
}
?>
The echo lines in the model there tell me that 2 rows are being returned form the query (which is spot on!), but I just can't seem to use the $query in the view properly.
I'm using $autoload['libraries'] = array('database'); in my autoload.php file and scaffolding seems to work fine, so I'd guess there's no problem in the database.php file.
Any advice greatly appreciated!