Welcome Guest, Not a member yet? Register   Sign In
Create news items Tutorial Not Working
#1

Hi guys

I'm a newbie in codeigniter and has been following the tutorial until I hit death end in the last tutorial 'Create New Item'. 

I tried to insert data into MySQL:
http://localhost/codeIgniter/index.php/news/create
and hit error:  
The webpage cannot be found HTTP 404


I tried to update my config.php from
Code:
$config['base_url'] = 'http://localhost/codeIgniter/';
to
Code:
$config['base_url'] = '';
but still the same
My routes.php:
Code:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = "pages/view";
$route['404_override'] = '';

My controller News.php
Code:
<?php
class News extends CI_Controller {
        public function __construct()
        {
                parent::__construct();
                $this->load->model('news_model');
                $this->load->helper('url_helper');
        }
        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 = NULL)
{
        $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');
    }
}
}

My model News_model.php
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);
}
   }

My view create.php
Code:
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
    <label for="title">Title</label>
    <input type="input" name="title" /><br />
    <label for="text">Text</label>
    <textarea name="text"></textarea><br />
    <input type="submit" name="submit" value="Create news item" />
</form>
LoadModule rewrite_module modules/mod_rewrite.so is enable
Thanks in advance!
Reply


Messages In This Thread
Create news items Tutorial Not Working - by aarontan78 - 01-29-2018, 02:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB