[英]News system CodeIgniter
我嘗試從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
我不知道這是什么問題。 但是我認為它可能會在route.config中出現,因為我只有: $route['default_controller'] = 'login';
->這是我的起始頁
但是在CodeIgniter教程中是:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
但是當我從3首行中添加一些內容時,即使新聞列表的第一頁也不會打開。
解決:我犯了愚蠢的錯誤。 因為控制器名稱為news,但函數為:public function view($art)
,鏈接應為: 'news/view/'.$news_item['art']
。
我認為以下鏈接有問題
<a href="news/<?php $news_item['art'] ?>">View article</a>
改用codeigniter錨點
anchor('news/'.$news_item['art'],'View article');
試試這個,喂我回來
您在以下位置忘記了echo
:
<a href="news/<?php $news_item['art'] ?>">View article</a>
您應該使用:
<a href="news/<?php echo $news_item['art'] ?>">View article</a>
要么:
<?php echo anchor('news/' . $news_item['art'], 'View article');
要么:
<?php echo anchor("news/{$news_item['art']}", 'View article');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.