ReeceSayer Posted February 22, 2012 Share Posted February 22, 2012 My first time using CI and i've run into a snag at the second hurdle (even though it seems quite common from my online searches) I'm completing the second tutorial from the user guide (http://codeigniter.com/user_guide/tutorial/news_section.html) The issue i'm having is similar to that i've seen posted on the CI forums but i'm not seeing any actual response on there. Model (news_model.php): <?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(); } //this seems to be causing an issue $query = $this->db->get_where('news', array('slug' => $slug)); return $query->row_array(); } } Controller (news.php): <?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'); } } I commented in the model where i think is causing an issue but i'm not sure why. Obviously i'm getting thrown into the 404 error because the data news item is empty but it shouldn't be. The initial index page works and my routes are all defined as they are in the tutorial. I previously replaced the show_404() with an exit('this is the error'); and it then shows this error. Also, my base url is defined as this: http://mysite/projects/framework which is where the CI installation is. Apologies if this has been answered before but when searching the forum i get an "Unable to access the search daemon" error. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/257498-codeigniter-tutorial-trouble/ Share on other sites More sharing options...
ReeceSayer Posted February 24, 2012 Author Share Posted February 24, 2012 Not solved but i've moved on cheers Quote Link to comment https://forums.phpfreaks.com/topic/257498-codeigniter-tutorial-trouble/#findComment-1320592 Share on other sites More sharing options...
I-AM-OBODO Posted March 19, 2012 Share Posted March 19, 2012 I had the same trouble with the tutorial and got stuck on the same new just like you. Although you have decided to move on but just in case you haven't gotten a solution pass the tutorials this is how mine was resolved. Model: the news_model.php file <?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); } } Controller: news.php <?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'); } } } Hope you find it useful. Bye Quote Link to comment https://forums.phpfreaks.com/topic/257498-codeigniter-tutorial-trouble/#findComment-1328952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.