我目前正在学习使用 CodeIgniter 的方法,并且在本教程的查看新闻部分需要一些帮助。它显示一个 404 页面。我的网站是Tristans.tk。单击新闻,然后单击其中一个标题。这是我的代码:
这是我的模型:
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_news($ID=FALSE){
if($ID===FALSE){
$query=$this->db->order_by("post_time", "DESC")->get('news');
return $query->result_array();
}
$query=$this->db->get_where('news',array('ID'=>$ID));
return $query->row_array();
}
}
这是新闻控制器:
class News extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('news_model');
}
public function index(){
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($ID){
$data['news_item'] = $this->news_model->get_news($ID);
if (empty($data['news_item'])){
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
这是视图:
<?php
print_r($news_item);
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['content'];
?>
路线:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
新闻索引:
<?php foreach ($news as $news_item): ?>
<div class='post'>
<h1 class='post_title'><a href="news/view/<?php echo $news_item['ID'] ?>"><?php echo $news_item['title'] ?></a></h1>
<p class='post_time'><?php echo $news_item['post_time'] ?></p>
<p class='post_content'><?php echo $news_item['content'] ?></p>
<span class='comment_link'><a href="news/view/<?php echo $news_item['ID'] ?>">Comment</a></span>
</div>
<?php endforeach ?>