If you are willing to learn how to create CRUD operations in Codeigniter 4 application. We will also shed light on how to integrate Bootstrap 4 and display data using Datatables jQuery plug-in.
Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web development. It contains CSS- and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components.
DataTables is a table enhancing plug-in for the jQuery Javascript library, adding sorting, paging and filtering abilities to plain HTML tables with minimal effort.
The following topics will be covered in this tutorial:
Invoke the first step by downloading the fresh Codeigniter 4 application using the composer package.
composer create-project codeigniter4/appstarter
Once the project has downloaded, rename the folder name as per your choice. In our case, we will name it codeigniter-crud-example. Afterward, go inside the project folder using the below command.
cd codeigniter-crud-example
You can also manually download the Codeigniter 4 application to impetus the CRUD operations development process.
This step is essential. Let me tell you why? When i was first learning Codeigniter, then i was getting stuck at every level, because i was not aware of how to debug the errors. After spending some time on google, i got to know that you can enhance the impetus of development process by enabling the error reporting and can display on-screen by following the below procedure.
Open app/Config/Boot/development.php file and set the display_errors
to 1 instead of 0. Repeat the same process in app/Config/Boot/production.php file.
ini_set('display_errors', '1');
In general, this step interprets how to give impetus to the database related work. We will make a new database demo
, create a username test
in MySQL, and establish the database connection.
To maintain the ongoing impulse, you can execute the SQL query from PHPMyAdmin to create the users table.
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
name varchar(100) NOT NULL COMMENT 'Name',
email varchar(255) NOT NULL COMMENT 'Email Address',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;
INSERT INTO `users` (`id`, `name`, `email`) VALUES
(1, 'John Doe', 'john@gmail.com'),
(2, 'Vanya Hargreeves', 'vanya@gmail.com'),
(3, 'Luther Hargreeves', 'luther@gmail.com'),
(4, 'Diego Hargreeves', 'diego@gmail.com'),
(5, 'Klaus Hargreeves', 'klaus@gmail.com'),
(6, 'Ben Hargreeves', 'ben@gmail.com'),
(7, 'The Handler', 'handler@gmail.com');
The subsequent work is to define the database details (db name, username, password) in the application/config/database.php file. It will concurrently make the coherence between Codeigniter and MySQL.
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'test',
'password' => '4Mu99BhzK8dr4vF1',
'database' => 'demo',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'development'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
CodeIgniter\Database\Exceptions\DatabaseException #8
Unable to connect database : Codeigniter
If anyhow you get the Codeigniter – cannot connect to MySQL database error, then change the hostname value based on your local server e.g MAMPP or XAMPP.
# MAMPP
public $default = [
...
'hostname' => '/Applications/MAMP/tmp/mysql/mysql.sock',
...
]
# XAMPP
public $default = [
...
'hostname' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
...
]
#codeigniter #mysql