昨天做完了《20分钟建立博客》的小例子,说是完成,其实就是照着视频,敲键盘。终于对codeigniter又有了点信心!
最终形成的三个PHP文件为:blog.php,comment_view.php,blog_view.php。
其中,控制器文件blog.php内容为:
<?php
class Blog extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
function index()
{
$data['title']="My Blog Title";
$data['heading']="My Blog Heading";
// $data['todo']=array('clean house','eat lunch','call mom');
$data['query']=$this->db->get('entries');
$this->load->view('blog_view',$data);
}
function comments()
{
$data['title']="My Comment Title";
$data['heading']="My Comment Heading";
$this->db->where('entry_id',$this->uri->segment(3));
$data['query']=$this->db->get('comments');
$this->load->view('comment_view',$data);
}
function comment_insert()
{
$this->db->insert('comments',$_POST);
redirect('blog/comments/'.$_POST['entry_id']);
}
}
?>
两个视图文件中的blog_view.php文件内容为:
<html>
<head>
<title><?=$title ?></title>
</head>
<body>
<h1><?=$heading?></h1>
<ol>
<?php foreach($query->result() as $row): ?>
<h3><?=$row->title?></h3>
<p><?=$row->body?></p>
<p><?=anchor('blog/comments/'.$row->id,'Comments'); ?></p>
<hr>
<?php endforeach; ?>
</ol>
</body>
</html>
两个视图文件中的comment_view.php内容为:
<html>
<head>
<title><?=$title ?></title>
</head>
<body>
<h1><?=$heading?></h1>
<ol>
<?php foreach($query->result() as $row): ?>
<h3><?=$row->title?></h3>
<p><?=$row->body?></p>
<p><?=anchor('blog/comments/'.$row->id,'Comments'); ?></p>
<hr>
<?php endforeach; ?>
</ol>
</body>
</html>