php - CodeIgniter news/view doesn't work, page 404's -
i learning ways codeigniter , need view news part of tutorial. shows 404 page. site tristans.tk. click on news , on 1 of titles. here code:
this model:
class news_model extends ci_model{ public function __construct(){ $this->load->database(); } public function get_news($id=false){ if($id===false){ $query=$this->db->order_by("post_time", "desc")->get('news'); return $query->result_array(); } $query=$this->db->get_where('news',array('id'=>$id)); return $query->row_array(); } } this news controller:
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($id){ $data['news_item'] = $this->news_model->get_news($id); 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'); } } and view:
<?php print_r($news_item); echo '<h2>'.$news_item['title'].'</h2>'; echo $news_item['content']; ?> the routes:
$route['news/(:any)'] = 'news/view/$1'; $route['news'] = 'news'; $route['(:any)'] = 'pages/view/$1'; $route['default_controller'] = 'pages/view'; news index:
<?php foreach ($news $news_item): ?> <div class='post'> <h1 class='post_title'><a href="news/view/<?php echo $news_item['id'] ?>"><?php echo $news_item['title'] ?></a></h1> <p class='post_time'><?php echo $news_item['post_time'] ?></p> <p class='post_content'><?php echo $news_item['content'] ?></p> <span class='comment_link'><a href="news/view/<?php echo $news_item['id'] ?>">comment</a></span> </div> <?php endforeach ?>
try change this
$route['news/(:any)'] = 'news/view/$1'; to
$route['news/(:any)'] = 'news/$1'; by using this: $route['news/(:any)'] = 'news/view/$1';,
if acces yourdomain.tk/news/view/1
that access yourdomain.tk/news/view/view/1
Comments
Post a Comment