【发布时间】:2016-05-23 05:49:08
【问题描述】:
我有一步一步地关注新闻部分的文档。在新闻中,CI 可以打开数据库中的数据。在数据库中有2个这样的数据 Put news data from database
在那里,有查看文章链接。但点击链接后,CI打开错误404这样 News Item from View article
可以帮我解决问题吗?对不起,我的英语不好
News_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
控制器(News.php)
<?php
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($slug)
{
$data['news'] = $this->news_model->get_news($slug);
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');
}
}
新闻视图中的Index.php
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
view.php 在视图中
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
【问题讨论】:
-
检查新闻控制器中是否存在名为Agung的函数
标签: php codeigniter web frameworks