CRUD (Create, Read, Update and Delete) Tutorial With CodeIgniter – Part 1

On this occasion we will discuss how to create CRUD (Create, Read, Update, and Delete) using the CodeIgniter Framework. We will learn how to view product lists and add/remove products owned by a store.

Crud Codeigniter

First of all, we need a database to store our data. I have prepared a database called db_product, in which there is a table msProduct which consists of fields / attributes productId, productName, stock. You can download the database file via this link: db_toko.sql

Folder structure that we will create:

crud_ci -> folder name
– application
– – controllers
– – – products.php -> located in the controllers folder
– – models
– – – products_model.php -> located in the models . folder
– – views
– – – products_view.php -> located in the views folder for product list view
– – – add_product_view.php -> is in the views folder for the display when inserting the product
– – – update_product_view.php -> is in the views folder to display when product updates

Because we need a connection to the database, there are several configurations that we need to do:

Setting autoload.php in the config folder to determine what things are loaded every time you open a page (there are helpers, libraries, etc., please see the user_guide codeigniter 🙂 )

$autoload[‘libraries’] = array(‘database’); //library that has been provided by codeigniter to make it easier for us to do things related to the database

$autoload[‘helper’] = array(‘url’); // helper for the url, so we can use base_url(), this base_url() will later generate your root folder, for example you use the url “localhost/tokosaya”, then localhost/tokosaya is your base_url()
after finishing with config.php, then we will continue to setting the database.php file in the config folder

$db[‘default’][‘hostname’] = ‘localhost’; // this is the hostname that we use to store our database, just leave it using localhost, because we use our own host that is on our computer
$db[‘default’][‘username’] = ‘root’; //this is the username used to access the database, default is root
$db[‘default’][‘password’] = ”; // the password used to access the database, the default value is empty, so if it is not set, then leave it as it is
$db[‘default’][‘database’] = ‘db_store’; // the name of the database that we use for this web application, this time I named it db_toko
After all the basic configuration has been completed, then we move on to the next stage, which is to create a controller that is used to handle page requests from users. We create a products.php file and then save it in the controllers folder (application/controllers/products.php). This products.php file will have a structure like this:

products.php :