CloudMind 发表于 2013-1-22 17:18:41

跟着用户指南做的“读取新闻条目”出错,百思不得其解

    今天照着用户指南上的教程-读取新闻条目 写代码,能显示所有新闻的记录页面,但是点击单个新闻记录View article,就显示404界面,鼠标放在链接上显示地址是正确的http://127.0.0.1/ci_demo/index.php/news/view/ajfkdjksdjsdf,ajfkdjksdjsdf是我在数据库里存进去的$slug值。
    C中代码:
    public function view($slug){
    $data['news_item'] = $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');
}

   求助各位啊~~

smallhe 发表于 2013-1-22 18:36:49

$data['news_item'] 应该不是为empty的吧。所以你的if不成立

CloudMind 发表于 2013-1-22 19:29:55

smallhe 发表于 2013-1-22 18:36 static/image/common/back.gif
$data['news_item'] 应该不是为empty的吧。所以你的if不成立

这个是判断有无数据取出的,empty用于判断,没有的话就显示404界面,用户指南上也是这样写的,这个逻辑好像没问题。

smallhe 发表于 2013-1-22 20:00:51

function “view” 是关键字吧

smallhe 发表于 2013-1-22 20:01:20

function “view” 是关键字么?换个名试试

CloudMind 发表于 2013-1-22 22:57:30

smallhe 发表于 2013-1-22 20:01 static/image/common/back.gif
function “view” 是关键字么?换个名试试

我查了,view不是关键字额。

smallhe 发表于 2013-1-23 10:53:52

不好意思,我也刚学几天,你把你的model贴出来看看吧

CloudMind 发表于 2013-1-23 11:39:22

全文model,都是照着用户指南上的写的~~
<?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();
        }
       
        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);
        }
}
?>

`NoCareFree` 发表于 2013-1-25 17:26:50

你加载了   $this->load->model('news_model'); 了吗?

CloudMind 发表于 2013-1-25 18:56:19

`NoCareFree` 发表于 2013-1-25 17:26 static/image/common/back.gif
你加载了   $this->load->model('news_model'); 了吗?

在控制器的构造函数里已经加载了的。
页: [1] 2 3
查看完整版本: 跟着用户指南做的“读取新闻条目”出错,百思不得其解