[eluser]Matteo[/eluser]
From a new CI user...
I was following the tutorials and got the first part of the news page to work, but when I click on the "slug" entry to go to the second single-post page, I get CI's 404 error.
My model code is as follows:
Code:
<?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();
}
}
My View code (/views/news/index.php) is as follows:
Code:
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a hre="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
and my view code for the single page (/views/news/view.php) is as follows:
Code:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
My controller code is:
Code:
<?php
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'] = $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');
}
}
Just for good measure, I have included my routes file as well...
Code:
*/
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
//$route['404_override'] = '';
I would have posted the HTML, but the forum won't let me...