Part 4 – Basic CRUD with Codeigniter

Codeigniter HMVC

In this fourth part of the series, we will implement a basic CRUD for Books. We will set up the controller to have a listing, edit and delete page. The model will do the database level activity and the views will show the listing form and the edit form also.

From here I will use the module structure – the so-called HMVC structure. If you are not sure on how to use this feature, then you can refer to this tutorial – Code Igniter and the HMVC architecture to work with modules. This is a great plugin for Codeigniter and this is the first one I add to a clean plate when I start a project.

So, let’s start with the controller. I have the index which redirects to the listing page. On the listing page, I have used the model’s get function without passing any id, so it will give me an array of all the available books.

Next three are the add, modify and the remove functions. The add and modify render view which has form but the remove function will only call the remove function from the model as per the id passed, delete the record and redirect to the listing page again.

The save and update function are the form submit pages where I have checked for the post data and passed the data to the model.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Books extends CI_Controller { // calling the constructor public function __construct() { parent::__construct(); $this->load->model('books/books_model', 'books');
  }
  
  public function index() {
    redirect('books/listing');
  }
  
  /**
   * This function will display the list of books
   * data coming from the model
   */
  public function listing() {
    $data['header']['title'] = 'Books listing';
    $data['footer']['scripts']['homescript.js'] = 'home';
    $data['view_name'] = 'books/books_listing_view';
    $data['view_data'] = $this->books->get();
  
    $this->load->view('page_view', $data);
  }
  
  /**
   * This function will display the form to add a new book
   */
  public function add() {
    $data['header']['title'] = 'Add a new book';
    $data['footer']['scripts']['homescript.js'] = 'home';
    $data['view_name'] = 'books/books_add_view';
    $data['view_data'] = '';
  
    $this->load->view('page_view', $data);
  }
  
  /**
   * This function will display the form for editing a book
   * the get function used to fetch the book info
   * [If no id, then it should display error]
   * @param int $id
   */
  public function modify($id = null) {
    if ($id == null) {
      show_error('No identifier provided', 500);
    }
    else {
      $data['header']['title'] = 'Edit a book';
      $data['footer']['scripts']['homescript.js'] = 'home';
      $data['view_name'] = 'books/books_edit_view';
      $data['view_data'] = $this->books->get($id);
  
      $this->load->view('page_view', $data);
    }
  }
  
  /**
   * This function deletes a book from the database
   * and redirects back to the listing
   * @param int $id
   */
  public function remove($id = null) {
    if ($id == null) {
      show_error('No identifier provided', 500);
    }
    else {
      $this->books->remove($id);
      redirect('books/listing'); // back to the listing
    }
  }
  
  /**
   * This function will call the model add function
   * and add the new book.
   */
  public function save() {
    if (isset($_POST) && $_POST['save'] == 'Add') {
      $data['name'] = $this->input->post('book_name');
      $data['price'] = $this->input->post('book_price');
      $data['author_id'] = $this->input->post('book_author_id');
  
      $this->books->add($data);
      redirect('books/listing'); // back to the add form
    }
    else {
      redirect('books/listing');
    }
  }
  
  /**
   * This function will update the info of the existing book
   */
  public function update() {
    if (isset($_POST) && $_POST['save'] == 'Save') {
      $data['book_id'] = $this->input->post('book_id');
      $data['name'] = $this->input->post('book_name');
      $data['price'] = $this->input->post('book_price');
      $data['author_id'] = $this->input->post('book_author_id');
  
      $this->books->add($data);
      redirect('books/listing'); // back to the add form
    }
    else {
      redirect('books/listing');
    }
  }
}

Then comes the model, here I have the get function which does two thing – one give me a list of all the records if the id is not passed. And if the id is passed then it will give me a row of result based on the id passed.

The other two are delete and add. The delete function is a simple removal of the record from the table based on the id passed. The add function (i know the name could have been better) does two things. If the id is present, will do an update or else it knows that it is an insert.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Books_model extends CI_Model { public function __construct() { parent::__construct(); } /** * This funtion takes id as a parameter and will fetch the record. * If id is not provided, then it will fetch all the records form the table. * @param int $id * @return mixed */ public function get($id = null) { $this->db->select()->from('books');
  
    // where condition if id is present
    if ($id != null) {
      $this->db->where('book_id', $id);
    }
    else {
      $this->db->order_by('book_id');
    }
  
    $query = $this->db->get();
  
    if ($id != null) {
      return $query->row_array(); // single row
    }
    else {
      return $query->result_array(); // array of result
    }
  }
  
  /**
   * This function will delete the record based on the id
   * @param $id
   */
  public function remove($id) {
    $this->db->where('book_id', $id);
    $this->db->delete('books');
  }
  
  /**
   * This function will take the post data passed from the controller
   * If id is present, then it will do an update
   * else an insert. One function doing both add and edit.
   * @param $data
   */
  public function add($data) {
    if (isset($data['book_id'])) {
      $this->db->where('book_id', $data['book_id']);
      $this->db->update('books',$data); // update the record
    }
    else {
      $this->db->insert('books', $data); // insert new record
    }
  }
}

The rest of the code related to this tutorial is are three views which I have used – and that you can get on git hub under this link: Github

Codeigniter CRUD

Codeigniter CRUD