2015-11-19 54 views
0

嘿家伙即时通讯新闻教程学习codeigniter和即时通讯。我几乎完成了,但我的观点方法显示404而不是新闻本身。我试着用下面的代码Codeigniter新闻教程查看方法不起作用

echo '<pre>'; 
    var_dump($this->news_model->get_news($slug)); 
    exit(); 

调试并返回

NULL 

我的继承人控制器是如何工作的,多数民众赞成在调用该方法

<?php 
class News extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->model('news_model'); 
    } 

    public function index() { 

     //echo '<pre>'; 
     //var_dump($this->news_model->get_news()); 
     //exit(); 

     $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) { 
     //echo '<pre>'; 
     //var_dump($this->news_model->get_news($slug)); 
     //exit(); 
     $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'); 

    } 

} 

IM仍然是一个初学者,所以我的调试解决方案有限。

+0

哪个版本的CI是你使用? –

+0

我认为你的新闻模型中的get_news()方法可能是罪魁祸首。此外,你应该真的将$ slug变量设置为null或其他一些默认值,以防止页面访问时没有一个slug的错误。 公共职能视图($塞= NULL){ ... } – twistedpixel

+0

@NiranjanNRaju IM学习通旧版本 –

回答

1
$data['news'] = $this->news_model->get_news($slug); 

应该根据自己的代码的其余部分是

$data['news_item'] = $this->news_model->get_news($slug); 

+0

lmao,我只是回头看看教程,这是我的一个错字...... –

+0

它发生在我们所有人:) – twistedpixel

+0

this仍然没有工作 –

0

不使用$ slug,当从set_news插入数据时,教程$ slug设置为true,但我不明白为什么属性类型varchar slug可以保存布尔类型。这里的代码将变量$ slug和所有属性设置为db。这里的控制消息的代码

public function set_news() 
    { 
     $this->load->helper('url'); 

     $slug = url_title($this->input->post('title'), 'dash', TRUE); 

     $data = array(
      'title' => $this->input->post('title'), 
      'slug' => $slug, 
      'text' => $this->input->post('text') 
     ); 

     return $this->db->insert('news', $data); 
    } 

编辑方法观的代码

public function view($slug = NULL) 
    { 
      $data['news'] = $this->news_model->get_news(); 
      //echo print_r($data['news_item']['0']['title'], true); 

      if (empty($data['news'])) 
      { 
        show_404(); 
      } 

      $data['title'] = $data['news']['0']['title']; 

      $this->load->view('templates/header', $data); 
      $this->load->view('news/view', $data); 
      $this->load->view('templates/footer'); 
    } 

和编辑的意见/新闻/ view.php到

<?php foreach ($news as $news_item): ?> 
    <h3><?php echo $news_item['title']; ?></h3> 
    <div class="main"> 
      <?php echo $news_item['text']; ?> 
    </div> 
    <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p><?php endforeach; ?>