Search code examples
phpcodeignitermamp

Codeigniter news tutorial view method not working


Hey guys im learning codeigniter and im on the news tutorial. Im pretty much finished but my view method is showing 404 rather than the news itself. I've tried to debug with the following code

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

and that returns

NULL

heres how my controller works thats calling the method

<?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 still a beginner so my debugging solutions are limited.


Solution

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

    should be

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

    according to the rest of your code.