Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Tutorial News section
#1

I am now studying the News section - user_guide\tutorial\news_section.html - and having to spend many hours trying to understand, and where I expect I should be spending only around an hour on such simple instructions.


(1) The 1st is clear. - Open up the application/models directory and create a new file called News_model.php and add the following code.
(2) The 2nd is difficult to understand. - Connect to your database and run the SQL command below. Also add some seed records. - How do I connect to the database and how do I run the SQL command? I assume the command is to be pasted into News_model.php
(3) The 3rd is partially clear. - Add the following code to your model. - I assume the code is to be pasted into News_model.php
(4) The 4th is partially clear. - Create the new controller at application/controllers/News.php. - I assume the code is to be pasted into News.php
(5) The 5th is difficult to understand. - The next thing to do is passing this data to the views. - I assume the code is to be pasted into News.php
(6) The 6th is clear. - Create application/views/news/index.php and add the next piece of code.
(7) The 7th is difficult to understand. - Go back to the news controller and update view() with the following: - I assume the code is to be pasted into News.php but don't understand why there are 2 loads.
(8) The 8th is clear. - The only things left to do is create the corresponding view at application/views/news/view.php. Put the following code in this file.
(9) The 9th is clear. - Modify your routing file (application/config/routes.php) so it looks as follows.

After following those instructions and assumptions, and pointing my browser to http://localhost/MyProject/index.php/news I can only get an error page - The website cannot display the page - HTTP 500

This is the code in my News_model.php page

Code:
<?php
class News_model extends CI_Model
{

       public function __construct()
       {
               $this->load->database();
       }

       CREATE TABLE news (
       id int(11) NOT NULL AUTO_INCREMENT,
       title varchar(128) NOT NULL,
       slug varchar(128) NOT NULL,
       text text NOT NULL,
       PRIMARY KEY (id),
       KEY slug (slug)
);

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();
}
}
This is the code in my News.php page
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();
       }

       public function view($slug = NULL)
       {
               $data['news_item'] = $this->news_model->get_news($slug);
       }

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

I have tried many variations, and now exhausted, and need guidance. Can anybody help?
Reply


Messages In This Thread
CodeIgniter Tutorial News section - by christaliise - 07-27-2015, 09:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB