Codeigniter news section

Here is a basic example of how you can create a news section in CodeIgniter:

Step 1: Create a new controller

In the application/controllers directory, create a new file called News.php. In this file, add the following code:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class News extends CI_Controller {

    public function index()
    {
        $this->load->model('news_model');
        $data['news'] = $this->news_model->get_news();
        $this->load->view('news/index', $data);
    }

    public function view($id)
    {
        $this->load->model('news_model');
        $data['news'] = $this->news_model->get_news_by_id($id);
        $this->load->view('news/view', $data);
    }

}

This controller has two methods: index and view. The index method loads the news model and retrieves all the news articles, then passes them to the index view. The view method loads the news model and retrieves a specific news article by ID, then passes it to the view view.

Step 2: Create a news model

In the application/models directory, create a new file called News_model.php. In this file, add the following code:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class News_model extends CI_Model {

    public function get_news()
    {
        $query = $this->db->get('news');
        return $query->result();
    }

    public function get_news_by_id($id)
    {
        $query = $this->db->get_where('news', array('id' => $id));
        return $query->row();
    }

}

This model has two methods: get_news and get_news_by_id. The get_news method retrieves all the news articles from the database and returns them as an array. The get_news_by_id method retrieves a specific news article by ID and returns it as an object.

Step 3: Create the views

In the application/views directory, create two new files: index.php and view.php.

index.php

<?php

echo "<h1>News</h1>";

foreach ($news as $article) {
    echo "<h2>". $article->title. "</h2>";
    echo "<p>". $article->content. "</p>";
    echo "<p><a href='". site_url('news/view/'. $article->id). "'>Read more</a></p>";
}

?>

This view displays a list of all the news articles, with a title, content, and a link to read more.

view.php

<?php

echo "<h1>". $news->title. "</h1>";
echo "<p>". $news->content. "</p>";

?>

This view displays a single news article, with a title and content.

Step 4: Create the database tables

Create two tables in your database: news and categories. The news table should have the following columns:

The categories table should have the following columns:

Step 5: Add data to the database

Add some sample data to the news and categories tables. For example:

Step 6: Test the news section

Open your web browser and navigate to http://yourdomain.com/news. You should see a list of all the news articles. Clicking on a news article should take you to a page with the article's title and content.

This is a basic example of how you can create a news section in CodeIgniter. You can customize and extend this example to fit your specific needs.