2012-10-30 97 views
0

我试着从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']

+0

嗯,首先你忘了'echo'在''View article,而不是''View article这 – manix

回答

0

你忘了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'); 
1

我认为问题与以下链接

<a href="news/<?php $news_item['art'] ?>">View article</a> 

使用笨锚,而不是

anchor('news/'.$news_item['art'],'View article'); 

试试这个,喂我回来

+0

是个好主意。我添加:'<?php echo anchor('news /'.$ news_item ['art'],'View article','class =“link class'')?>'当我点击链接(链接很好),但我有错误:'页面未找到404' – praashaad

+0

谢谢你一切都好!我犯了愚蠢的错误。因为控制器名称消息,但是函数:public function'view($ art)',并且链接应该是:'news/view /'.$ news_item ['art']'。感谢形式的帮助,加上你!谢谢 – praashaad