[eluser]mrmu[/eluser]
Hi all,
I'm a CodeIgniter beginner and I got some problems while I tried to re-write the blog example of CodeIgniter tutorial video.
I want to use "load model" to demo a simple MVC structure, but I failed, there is nothing shown on my screen but a full blank.
I'd like to post the codes and hope someone can help me!
First, I create a database called "test" with a table "entries" (includes id, title, body 3 columns) as the blog example and I also add 2 records in it.
I also set this in the config/autoload.php:
Code:
$autoload['libraries'] = array('database');
{Controller}
blog.php
it can work if I mark B,C without A.)
Code:
<?php
class Blog extends Controller
{
function __construct()
{
parent::Controller();
}
function index()
{
$data['title'] = "Title";
$data['heading'] = "My Page";
// $data['query'] = $this->db->get('entries'); // A
$this->load->model('blog_model'); // B
$data['query'] = $this->blog_model->getdata(); // C
$this->load->view('blog_view', $data);
}
}
?>
{Model}
blog_model.php:
Code:
<?php
class Blog_model extends Model {
function Blog_model()
{
parent::Model();
}
function getdata()
{
$query = $this->db->get('entries');
return $query;
}
?>
{View}
blog_view.php:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $heading ?></h1>
<?php foreach($query->result() as $row): ?>
<h3><?php echo $row->title ?></h3>
<p><?php echo $row->body ?></p>
<hr>
<?php endforeach; ?>
</body>
</html>
Thank you!!