我有新闻部分的逐步文档。在新闻中,CI可以从数据库中打开数据。在数据库中有 2 个这样的数据从数据库中获取新闻数据
在那里,有查看文章链接。 但是单击链接后,CI像这样打开错误404 来自"查看"文章的新闻项
可以帮助我解决问题吗? 对不起,我的英语不好
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();
}
}
控制器(新闻.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');
}
}
索引.php in 新闻 查看
<?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 ?>
视图.php在视图中
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
您尚未完成本教程末尾的"路由"部分。
另外,不要开始使用CodeIgniter 2.x开始学习 - 它不再受支持 - 而是使用版本3。