Here’s my model:
<?php
class Dbtest extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
parent::Model();
$this->db->select('title, content');
$this->db->from('posts');
$this->db->where('post_id', 1);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
Here’s my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dbtest extends CI_Controller {
function index() {
$this->load->model('dbtest');
$data['rows'] = $this->dbtest->getAll();
$this->load->view('dbtest', $data);
}
}
And here’s my view
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="container">
<p>My view has been loaded</p>
<?php foreach($rows as $r) : ?>
<h1><?php echo $r->title; ?></h1>
<div><?php echo $r->content; ?></div>
<?php endforeach; ?>
</div>
</body>
</html>
I keep getting a 500 server error. What could I possibly be doing wrong. I have autoloaded the database library. Please help a codeigniter newbie trying to learn this great framework.
After taking a quick look, why is this line there?
It is a php4 constructor call to the old version of the Model base class that no longer exists..
remove and try again.
EDIT
Also, you cannot name a model AND Controller the same name, they have namespace conflicts.
call the model
Dbtest_modelor something and use it that way.Also, this is unnecessary
$q->result()returns an array, no need to loop through and rebuild… Just do this…