• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » Simple CRUD Apps With Codeigniter Tutorial For Beginners

Simple CRUD Apps With Codeigniter Tutorial For Beginners

By Sigit Prasetya Nugroho ∙ July 19, 2017 ∙ PHP ∙ 8 Comments

Share : TwitterFacebookTelegramWhatsapp

Continuing my two previous CodeIgniter tutorial articles. On this occasion, I will discuss how to create a simple CRUD application using CodeIgniter framework for beginners. Codeigniter is a PHP framework that is suitable for beginner. All libraries for develop application have been provided by CodeIgniter. The concept of MVC on CodeIgniter allows a beginner to code neatly and regularly. Follow the following tutorial.

In the previous article, I have discussed the basic knowledge of what is CodeIgniter and MVC concept on CodeIgniter. If you are a beginner who learning about this PHP framework, it’s good first to read my previous article to follow this article correctly.

To understand more detail our CodeIgniter tutorial, we will make a simple CRUD application to display product list, add, edit and delete data in the product database. In this CodeIgniter tutorial, will be divided into several step like the following table of contents.

Table of Contents

  • 1 Codeigniter tutorial for beginners step by step
    • 1.1 Installing Codeigniter
    • 1.2 Configure MYSQL database on CodeIgniter using PDO – Codeigniter Tutorial
    • 1.3 Create database “pos” in MySQL and add data in the product table
    • 1.4 Displays the product list
    • 1.5 Create a new product
    • 1.6 Update item data
    • 1.7 Delete product data

Codeigniter tutorial for beginners step by step

Installing Codeigniter

I have discussed in full how to install CodeIgniter in the article “Introduction – PHP Codeigniter Tutorial For Dummies Step By Step.” Please download Master CodeIgniter on the official site. Then extract and create a new project named mycodeigniter.

Related Articles :

  • CodeIgniter Tutorial How To Make Rest API ( Server And Client )
  • How To Remove Index php Url Path Codeigniter
  • PHP Codeigniter MVC Concept For Dummies With Simple Example

Next step we will do the basic configuration of CodeIgniter. Please open the config.php file in the “application/config” folder and do the settings according to the following script:

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

Make sure the installation has been successful and run the URL http://localhost/mycodeigniter like the following picture below:

PHP Codeigniter Tutorial Installation Successfull

Configure MYSQL database on CodeIgniter using PDO – Codeigniter Tutorial

Next, we will tell the CodeIgniter about what libraries we need during the development process. This configuration contained in the autoload.php file in the “application/config folder”.

1
2
$autoload['libraries'] = array('database');
$autoload['helper'] = array('url','form');

How to connect CodeIgniter and MySQL using PDO is quite easy. You just need to add the following settings to the database.php file in the folder (application/config/database.php).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
'dsn' => 'mysql:host=localhost; dbname=pos; charset=utf8;',
'hostname' => 'localhost',
'username' => 'root', // user db
'password' => '123456', // password db
'database' => '',
'dbdriver' => 'pdo', // pdo connection
'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
);

Create database “pos” in MySQL and add data in the product table

Create a new database with the name “pos.” Then create a new table with the name “m_item” with fields like the following picture:

Create Pos Database Mysql And M Item table

Add 2 data in the “m_item” table with the following MySQL script:

1
2
insert into 'm_item' ('id_item', 'item_name', 'note', 'stock', 'price', 'unit') values('PB0001','Razor Blade','best razor blade in the world','9.00','12000','PCS');
insert into 'm_item' ('id_item', 'item_name', 'note', 'stock', 'price', 'unit') values('PB0002','Muscle Face','for build body','10.00','50000','PCS');

Displays the product list

To interact with the product table we have to create a new model stored in the “application/model” folder, and please create a model with the name Product_model.php then type the following script

1
2
3
4
5
6
7
8
<?php
class Product_model extends CI_Model {
function list_product(){
$product = $this->db->query('select * from m_item');
return $product;
}
}
?>

When finished creating the model then the next step, we will create a controller that will connect to the model and view, the controller will be stored in the “application/controller” folder. Create a new controller named Product.php and write the following script:

1
2
3
4
5
6
7
8
9
<?php
class Product extends  CI_Controller {
public function index(){
$this->load->model('product_model');
$data['product_list'] = $this->product_model->list_product()->result();
$data['title'] = "Product";
$this->load->view('product_list',$data);
}
}

Now that the model and controller is done, the final step is to build a view to display the data to the application user, and this view is stored in the “application/view” folder. Create a view with the name of product_list.php and type the following script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<h2>List of <?php echo $title; ?></h2>
<hr />
<?php echo anchor('product/add','NEW PRODUCT'); ?>
<br/>
<br>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>NOTE</th>
<th>PRICE</th>
<th>STOCK</th>
</tr>
</thead>
<tbody>
<?php foreach ($product_list as $list) { ?>
<tr>
<td><?php echo $list->id_item ?></td>
<td><?php echo $list->item_name ?></td>
<td><?php echo $list->note ?></td>
<td><?php echo $list->price ?></td>
<td><?php echo $list->stock.' '.$list->unit ?></td>
</tr>
<?php } ?>
</tbody>
</table>

Please try at your browser with url: http://localhost/mycodeigniter/index.php/product/

If successful then the system will display the product list as shown below.

Create Crud Codeigniter Tutorial How To Display List Item Min

Create a new product

The data-added process requires two functions on the controller:

add function:
The task is to invoke the view to display the input form.

save function:
The task is to store data inputted by the user and submitted to the model to saved in the database

Add the following script to the Product.php controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public function add()
{
$this->load->view('product_form');
}
public function save()
{
$array_item = array(
'id_item' => $this->input->post('item_id'),
'item_name' => $this->input->post('item_name'),
'note' => $this->input->post('item_note'),
'stock' => $this->input->post('item_stock'),
'price' => $this->input->post('item_price'),
'unit' => $this->input->post('item_unit')
);
$this->load->model('product_model');
$this->product_model->save($array_item);
redirect('product');
}

To communicate with the database, create new function in the product_model.php to save new data to “m_item” table.

1
2
3
4
function save($array)
{
$this->db->insert('m_item',$array);
}

Up to this point, the display of our application will be as follows:

Codeigniter Tutorial How To Add New Item Result

Update item data

The two processes will be discussed at this step is the process of retrieve data from the database and update process to change the existing records.

Like my previous CRUD tutorial, where the user will select the product to be edited. Then customize the product with the new data and then the save process will update the database.

The update process requires two functions on the controller product:
edit function:
Used to display the edit product form

save_edit function:
This function stores the result of data changes and passes it to the model to update the items in the database.

Copy the following script to create update controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public function save_edit()
{
$id = $this->input->post('item_id');
$array_item = array(
'item_name' => $this->input->post('item_name'),
'note' => $this->input->post('item_note'),
'stock' => $this->input->post('item_stock'),
'price' => $this->input->post('item_price'),
'unit' => $this->input->post('item_unit')
);
$this->load->model('product_model');
$this->product_model->update($id,$array_item);
redirect('product');
}
public function edit(){
$this->load->model('product_model');
$data['product'] = $this->product_model->product($this->uri->segment(3))->row_array();
$this->load->view('product_edit',$data);
}

Create a new view as product_edit.php to display the edit form. Copy the following script to product_edit.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php echo form_open('product/save_edit') ?>
<?php echo form_hidden('item_id',$product['id_item']) ?>
<table>
<tr>
<td>NAME</td>
<td><?php echo form_input('item_name',$product['item_name'],array('placeholder'=>'Name')) ?></td>
</tr>
<tr>
<td>NOTE</td>
<td><?php echo form_textarea('item_note',$product['note'],array('placeholder'=>'Note')) ?></td>
</tr>
<tr>
<td>PRICE</td>
<td><?php echo form_input('item_price',$product['price'],array('placeholder'=>'Price')) ?></td>
</tr>
<tr>
<td>STOCK</td>
<td><?php echo form_input('item_stock',$product['stock'],array('placeholder'=>'Stock')) ?></td>
</tr>
<tr>
<td>UNIT</td>
<td><?php echo form_input('item_unit',$product['unit'],array('placeholder'=>'Unit Name')) ?></td>
</tr>
<tr>
<td colspan="2"> <hr><?php echo form_submit('submit', 'Update Item!'); ?> <?php echo anchor('product','Back') ?></td>
</tr>
</table>
<?php echo form_close(); ?>

Create update function on Product_model.php  to update data in the database:

1
2
3
4
5
function update($id,$array_item)
{
$this->db->where('id_item',$id);
$this->db->update('m_item',$array_item);
}

The next step is edit your product_list.php view and add the following script:

1
<td> <?php echo anchor('product/edit/'.$list->id_item,'Edit') ?> </td>

Explanation:

Anchor is a function in the helper URL in Codeigniter  library used to create a link.

Until  this step the display of our application as shown below:

Codeigniter Tutorial How To Update The Item Result

Delete product data

In the last section of this CodeIgniter tutorial, I will explain how the process of deleting data from the database.

To delete the data just needed one function on the controller and one function on the model. Please copy the script below to the Product.php controller:

1
2
3
4
5
6
7
public function delete()
{
$id = $this->uri->segment(3);
$this->load->model('product_model');
$this->product_model->delete($id);
redirect('product');
}

And please copy the script below and paste in the Product_model.php

1
2
3
4
5
function delete($id)
{
$this->db->where('id_item',$id);
$this->db->delete('m_item');
}

The last step is create anchor to execute the delete function, please add the following script to product_list.php view.

1
<td> <?php echo anchor('product/edit/'.$list->id_item,'Edit') ?> || <?php echo anchor('product/delete/'.$list->id_item,'Delete') ?></td>

Please save and run the application in your browser.

Codeigniter Tutorial How To Create Delete Function To Delete Record Of Table In Mysql Database

Up to this point, the CodeIgniter tutorial for beginners with CRUD examples has been completed. If you have some advice or want to ask question for me, please fill the comment form below.

To download the entire project please use the following url:

 

[sociallocker id=58]

URL : Download codeIgniter tutorial for beginners example code

password: seegatesite.com

[/sociallocker]

Thus my article about tutorial how to create simple CRUD apps with codeigniter for beginner, hope useful.

Another PHP Related Post :

  • Tutorial Create Simple POS Using ReactJS And Laravel Lumen Part 1
  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for saaronbonsaaronbon says

    September 7, 2018 at 6:07 am

    Hi,

    Where’s your code and form for the “Add New Product”?

    Thanks

    Reply
    • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

      September 10, 2018 at 11:56 am

      please download the source code project

      Reply
      • Avatar for LuqmanLuqman says

        October 15, 2018 at 10:02 am

        where is the download link for the source code? this link “http://wp.me/a65Dpx-Vs” always redirect to this page.

        Reply
        • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

          October 19, 2018 at 1:42 am

          https://seegatesite.com/wp-content/uploads/2017/07/Simple-CRUD-Apps-With-Codeigniter-Tutorial-For-Beginners.zip

          Reply
  2. Avatar for formwareformware says

    January 12, 2019 at 8:51 am

    what’s the password

    Reply
    • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

      January 19, 2019 at 9:20 am

      seegatesite.com

      Reply
  3. Avatar for CristianCristian says

    April 2, 2019 at 5:39 am

    Excelent tutorial!!..

    Reply
  4. Avatar for SaifulSaiful says

    October 27, 2020 at 8:06 pm

    Thank you. Excellent tutorial . You saved my life 🙂

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for SaifulThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) lumen api (4) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (8) Publisher Tips (5) react (6) Reactjs (9) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2022 Seegatesite.com