具体数据不会出现在codeigniter中

i am doing some practice on the codeigniter to retrieve the data from database and i am success to do this.but the problem arise when i want to fetch data of a specific field. to retrieve the specific value i am using the following URL on my local host:

localhost/codeigniter/index.php/news/view/city-news

where news is controller,view is method of controller and city-news is argument.

Here is my code of controller:

public function view($slug)
    {

        $data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
                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');
    }

this method calls the method get_news($slug) of model news_model.here is the code of this method:

public function get_news($slug = FALSE)
   {

    if ($slug === FALSE)
    {
        $query = $this->db->get('news');
        return $query->result_array();
    }
    echo $slug;//here is i m also getting the slug value.
    $query = $this->db->get_where('news', array('slug' => $slug));//i think this is not working properly
    print_r($query->row_array());die;//now i am getting values here.   
    return $query->row_array();
   } 

but still my view shows "404 page not found". my view code is:

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

Now please tell me where i am going wrong.

your error is you are setting $data['news'] and testing $data['news_item'] :

$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.

if (empty($data['news_item'])) // <=== HERE IS THE ERROR
{
    show_404();
}

die() is a function use exit instead.

set this at the beginning of script

ini_set('display_errors', 1);
error_reporting(E_ALL);

also use var_dump($query); to check if it return any result if not exception or fatal error was raised when calling query

if array is empty it means that no news with this slug is in db. You should set some conditions when particular news is not found

also what is the value of this slug, does it contain any special chars etc.?

have you tried with simple where query as follows

$data = $this->db->where('slug', $slug)->from('news');

you must get something in $data. Means at least mysql object. Its been another part that whether rows are available in database or not.

secondly i am worried about the true and false. Means as per i know Boolean store as INT(1) means either 0 or 1. You needs to check with that as well.

you can debug your query with

$this->db->last_query();

Hope this helps.

Here is the cleaned code of the controller:

public function view($slug)
{

    $news = $this->news_model->get_news($slug);
            if (empty($news))
        {
    show_404();
        }

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

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

this method calls the method get_news($slug) of model news_model:

public function get_news($slug = FALSE)
{

if ($slug === FALSE)
{
    $query = $this->db->get('news');
    return $query->result_array();
}
echo $slug;
$query = $this->db->get_where('news', array('slug' => $slug));
print_r($query->row_array());die;//now i am getting values here.   
return $query->row_array();

}

<?php echo '<h2>'.$title.'</h2>'; echo
$text; ?>

A small error: Here

if (empty($data['news_item'])) // <=== put "news" here
{
    show_404();
}

and at your view try to get data as

echo $news;