为什么我不能得到任何数据到我的codeigniter视图


Why wont i get any data to my codeigniter view?

我以我的问题开始:

  1. 为什么我在视图中没有收到游骑兵队文章的数据?
  2. 为什么路径:http://igniter.se/index.php/news/view/Rangers不工作?我认为这是它应该如何工作:控制器/方法在模型/属性?

我在ellislab.com News_Section上学习第二篇教程。我也看到其他人在这个教程中有一些问题,但我没有发现我遇到的同样的问题…

我的问题是,当我从所有新闻导航到只有一个(在一个href上)时,我没有收到任何数据。

在此路径将显示db中的所有新闻:

http://ignitertut.se/index.php/news/

在这些路径(例如),我将得到一个404:

http://ignitertut.se/index.php/news/view/, http://ignitertut.se/index.php/news/view/Rangers/

但是在这个路径,我将得到一个空页:没有数据,没有标题& &;脚模板包括,没有标题在头部通常自动得到一个。

http://ignitertut.se/index.php/news/Ragers/

Where Rangers是一篇关于Rangers的文章,存储在DB中。

模型:

<?php
class News_model extends CI_Model {
    public function __construct()
    {
        //Preloaded db
    }
    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();
    }
}

包含所有文章的View - "index.php"

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

特定文章的视图- " View .php"

<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo '<p>' .$news_item['text']. '</p>';

控制器

class News extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('news_model');
    }
    public function index()
    {
        $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)
    {
        $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');
    }
}
<<p> 路线/strong>

$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

我已经自动加载数据库并设置:$config['base_url'] = 'http://ignitertut.se/';(本地运行)

要调用的单词是url参数。在codeigniter中有一种获取url参数的方法。

为了工作你现有的代码更改控制器函数的视图为这个

public function view()
    {
        $slug = $this->uri->segment(2);
        $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');
    }

详细信息请参考此urlhttp://ellislab.com/codeigniter/user-guide/libraries/uri.html