CRUD operation in CodeIgniter – (codeigniter crud)

Today, We want to share with you codeigniter crud.In this post we will show you Insert Fetch Update Delete Mysql data in Codeigniter, hear for Codeigniter Active Record: Insert, Select, Update, Delete we will give you demo and example for implement.In this post, we will learn about CodeIgniter CRUD operation Example Tutorial From Scratch with an example.

crud operations in codeigniter bootstrap

Codeigniter 3 – Basic CRUD application with MySQL Example with Demo

Step 1: codeigniter download
here simple you can codeigniter download Step 1: Download Codeigniter 3

Step 2: Create a Database and Configuration

products table:

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `product_short_desc` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;

Example 3: application/config/database.php

 '',
	'hostname' => 'localhost',
	'username' => 'jay_dsp',
	'password' => 'Jaydeep@344895754',
	'database' => 'pakainfo',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

codeigniter route crud (Step 3: Create Routes)

application/config/routes.php
here you can simply codeigniter route crud


Step 4: Add ProductCRUDExample Controller

application/controllers/ProductCRUDExample.php

load->library('form_validation');
      $this->load->library('session');
      $this->load->model('ProductCRUDExampleModel');


      $this->productCRUDExample = new ProductCRUDExampleModel;
   }


   /**
    * Display Data this method.
    *
    * @return Response
   */
   public function index()
   {
       $data['data'] = $this->productCRUDExample->get_productCRUDExample();


       $this->load->view('theme/header');       
       $this->load->view('productCRUDExample/list',$data);
       $this->load->view('theme/footer');
   }


   /**
    * Show Details this method.
    *
    * @return Response
   */
   public function show($id)
   {
      $product = $this->productCRUDExample->find_product($id);


      $this->load->view('theme/header');
      $this->load->view('productCRUDExample/show',array('product'=>$product));
      $this->load->view('theme/footer');
   }


   /**
    * Create from display on this method.
    *
    * @return Response
   */
   public function create()
   {
      $this->load->view('theme/header');
      $this->load->view('productCRUDExample/create');
      $this->load->view('theme/footer');   
   }


   /**
    * Store Data from this method.
    *
    * @return Response
   */
   public function store()
   {
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('product_short_desc', 'Description', 'required');


        if ($this->form_validation->run() == FALSE){
            $this->session->set_flashdata('errors', validation_errors());
            redirect(base_url('productCRUDExample/create'));
        }else{
           $this->productCRUDExample->insert_product();
           redirect(base_url('productCRUDExample'));
        }
    }


   /**
    * Edit Data from this method.
    *
    * @return Response
   */
   public function edit($id)
   {
       $product = $this->productCRUDExample->find_product($id);


       $this->load->view('theme/header');
       $this->load->view('productCRUDExample/edit',array('product'=>$product));
       $this->load->view('theme/footer');
   }


   /**
    * Update Data from this method.
    *
    * @return Response
   */
   public function update($id)
   {
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('product_short_desc', 'Description', 'required');


        if ($this->form_validation->run() == FALSE){
            $this->session->set_flashdata('errors', validation_errors());
            redirect(base_url('productCRUDExample/edit/'.$id));
        }else{ 
          $this->productCRUDExample->update_product($id);
          redirect(base_url('productCRUDExample'));
        }
   }


   /**
    * Delete Data from this method.
    *
    * @return Response
   */
   public function delete($id)
   {
       $product = $this->productCRUDExample->delete_product($id);
       redirect(base_url('productCRUDExample'));
   }
}

Step 5: Create ProductCRUDExample Model

application/models/ProductCRUDExample.php

input->get("search"))){
          $this->db->like('title', $this->input->get("search"));
          $this->db->or_like('product_short_desc', $this->input->get("search")); 
        }
        $query = $this->db->get("products");
        return $query->result();
    }


    public function insert_product()
    {    
        $data = array(
            'title' => $this->input->post('title'),
            'product_short_desc' => $this->input->post('product_short_desc')
        );
        return $this->db->insert('products', $data);
    }


    public function update_product($id) 
    {
        $data=array(
            'title' => $this->input->post('title'),
            'product_short_desc'=> $this->input->post('product_short_desc')
        );
        if($id==0){
            return $this->db->insert('products',$data);
        }else{
            $this->db->where('id',$id);
            return $this->db->update('products',$data);
        }        
    }


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


    public function delete_product($id)
    {
        return $this->db->delete('products', array('id' => $id));
    }
}
?>

Step 6: Create View Files

  • 1) header.php
  • 2) footer.php
  • 3) list.php
  • 4) create.php
  • 5) show.php
  • 6) edit.php

application/views/theme/header.php




    Basic Crud operation in Codeigniter 3
    


application/views/theme/footer.php

  

application/views/productCRUDExample/list.php

Codeigniter 3 CRUD Example from scratch

Title Description Action
title; ?> product_short_desc; ?>
show Edit

application/views/productCRUDExample/create.php

Add New Product

session->flashdata('errors')){ echo '
'; echo $this->session->flashdata('errors'); echo "
"; } ?>
Title:
Description:

application/views/productCRUDExample/show.php

Show Product

Title: title; ?>
Description: product_short_desc; ?>

application/views/productCRUDExample/edit.php

Edit Product

session->flashdata('errors')){ echo '
'; echo $this->session->flashdata('errors'); echo "
"; } ?>
Title:
Description:

application/config/config.php

$config['base_url'] = 'http://localhost:8000';

I hope you get an idea about Multiple Inserts, Update, Delete using Multiple Select in Codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment