Welcome Guest, Not a member yet? Register   Sign In
A simple MVC sample, but cannot work well with loading model...
#1

[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.phpSadit 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">
&lt;html &gt;
&lt;head&gt;
  &lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;&lt;?php echo $title ?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?php echo $heading ?&gt;</h1>

&lt;?php foreach($query->result() as $row): ?&gt;

    <h3>&lt;?php echo $row->title ?&gt;</h3>
    <p>&lt;?php echo $row->body ?&gt;</p>
    <hr>

&lt;?php endforeach; ?&gt;

&lt;/body&gt;
&lt;/html&gt;

Thank you!!


Messages In This Thread
A simple MVC sample, but cannot work well with loading model... - by El Forum - 10-06-2009, 12:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB