我试着从http://ellislab.com/codeigniter/user_guide/tutorial/news_section.html
新闻系统笨
例如添加到我的网站,这是代码:
news_model
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($art = FALSE)
{
if ($art === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('art' => $art));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'art' => $art,
'text' => $this->input->post('text'),
'image'=> $image
);
return $this->db->insert('news', $data);
}
}
?>
news_controller
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['errors_login'] = array();
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('main/open_news', $data);
}
public function view($art) {
$data['news_item'] = $this->news_model->get_news($art);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['errors_login'] = array();
$this->load->view('main/open_one_news', $data);
}
}
open_news
<?php
$this->load->view('mains/header');
$this->load->view('main/news');
$this->load->view('mains/footer');
?>
新闻观
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<a href="news/<?php $news_item['art'] ?>">View article</a>
<?php endforeach ?>
当我在<a href="news/<?php $news_item['art'] ?>">View article</a>
点击页面未转发到新闻形而下页面,只有在链接复制 “新闻”:
http://localhost/index.php/news/news/news/news/news/news
我不知道什么是问题。但我认为这将可能在routes.config 因为我只有: $route['default_controller'] = 'login';
- >这是我的开始页面
但笨教程:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
但是当我添加一些来自3第一行,甚至包含新闻列表的第一页都没有打开。
求助: 我犯了愚蠢的错误。由于控制器名称消息,但功能:公共功能view($art)
,并且链接应该是:'news/view/'.$news_item['art']
。
嗯,首先你忘了'echo'在''View article,而不是''View article这 – manix