Welcome Guest, Not a member yet? Register   Sign In
Newbie Problem With News Example
#1

[eluser]Unknown[/eluser]
Hello everyone,

I'm new to CI and I can't go further to learn CI because I have a trouble with the news example.
I can create new news article but after I click on the 'view Article' button, i always get the 404 error.

So please can u have a look at my code.

Thank you so far lovely professionals ;-)


This is the news controller:

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_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');
}

public function create()
{
  $this->load->helper('form');
  $this->load->library('form_validation');

  $data['title'] = 'Create a news item';

  $this->form_validation->set_rules('title', 'Title', 'required');
  $this->form_validation->set_rules('text', 'text', 'required');

if ($this->form_validation->run() === FALSE)
{
  $this->load->view('templates/header', $data);
  $this->load->view('news/create');
  $this->load->view('templates/footer');

}
else
{
  $this->news_model->set_news();
  $this->load->view('news/success');
}
}
}

news-model:
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();
   }
  
   public function set_news()
   {
  $this->load->helper('url');
  
  $slug = url_title($this->input->post('title'),'dash', TRUE);
  
  $data = array
  (
   'title' => $this->input->post('title'),
   'slug' => $slug,
   'text'  => $this->input->post('text')
  );
  
  return $this->db->insert('news',$data);
   }
}
?>

index.php:

Code:
<?php foreach ($news as $news_item): ?>

    <h2>&lt;?php echo $news_item['title'] ?&gt;</h2>
    <div class="main">
        &lt;?php echo $news_item['text'] ?&gt;
    </div>
    <p><a href="news/view/&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p>

&lt;?php endforeach ?&gt;

view.php:

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

and the routes.php:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/

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


/* End of file routes.php */
/* Location: ./application/config/routes.php */
#2

[eluser]Unknown[/eluser]
Ok I found out in the index.php view when I change line into <p><a href="&lt;?php echo $news_item['slug'] ?&gt;">View article</a></p> then it works!




Theme © iAndrew 2016 - Forum software by © MyBB