Create Database
Create new database named codeigniter4_db. This database have 1 table: Product table.
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`price` double NOT NULL,
`quantity` int(11) NOT NULL,
`status` tinyint(1) NOT NULL,
`created` date NOT NULL,
`description` text NOT NULL,
`category_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`name`, `price`, `quantity`, `status`, `created`, `description`, `category_id`) VALUES
('Tivi 1', 20, 4, 1, '2021-07-13', 'good', 1),
('Tivi 2', 63, 15, 0, '2020-08-11', 'good', 1),
('Laptop 1', 24, 12, 1, '2021-08-12', 'good', 2),
('Laptop 2', 60, 34, 1, '2020-08-12', 'good', 2),
('Laptop 3', 23, 27, 0, '2020-08-17', 'good', 3),
('Computer 1', 4.5, 2, 1, '2021-08-20', 'ABC', 3),
('Computer 2', 111, 222, 1, '2021-09-25', 'AAA', 7),
('Computer 3', 33333, 888, 0, '2021-10-11', 'abc', 2),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7),
('ABC', 20, 4, 1, '2021-07-12', 'good', 7);
Structure of Product Table
Data of Product Table
Download and Install CodeIgniter 4
Download the latest version of CodeIgniter 4 and unzip source code to new folder named LearnCodeIgniter4WithRealApps
Cut index.php and htaccess files in public folder to root folder of project
Open index.php in root folder find to line 16 replace path to Paths.php file as below:
$pathsPath = realpath(FCPATH . '/app/Config/Paths.php');
Open App.php in app/Config folder find to line 39 remove index.php string in $indexPage variable as below:
public $indexPage = '';
Set BASE URL
Open App.php file in app/Config folder. Set value for $baseURL variable as below:
public $baseURL = 'http://localhost:8091/LearnCodeIgniter4WithRealApps/';
Configure Database Connections
Open Database.php file in app/Config folder and add values as below connect to database:
<?php
public $default = [
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter4_db',
'port' => 3306,
];
Create Controller
Create new PHP file named Demo.php in app/Controllers folder as below:
<?php
namespace App\Controllers;
class Demo extends BaseController
{
protected $table = 'product';
private $db;
public function __construct()
{
$this->db = \Config\Database::connect();
helper(['url']);
}
public function index()
{
return view('demo/index');
}
public function create()
{
$product = $this->request->getVar();
$product['status'] = $product['status'] != NULL;
$product['created'] = date('Y-m-d', strtotime($product['created']));
$this->db->table($this->table)->insert($product);
$data['latestId'] = $this->db->insertID();
$data['affectedRows'] = $this->db->affectedRows();
return view('demo/result', $data);
}
}
Create Views
Create new folder named demo in app/Views folder. In this folder, create new PHP file named as below:
Index View
In demo folder, create new PHP file named index.php as below:
<html>
<head>
<title>Query Builder Class in CodeIgniter 4</title>
</head>
<body>
<h3>Add New Product</h3>
<form method="post" action="<?= site_url('demo/create') ?>">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price"></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity"></td>
</tr>
<tr>
<td>Status</td>
<td><input type="checkbox" name="status"></td>
</tr>
<tr>
<td>Created</td>
<td><input type="text" name="created"> (MM/dd/yyyy)</td>
</tr>
<tr>
<td valign="top">Description</td>
<td><textarea cols="20" rows="5" name="description"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
Result View
In demo folder, create new PHP file named result.php as below:
<html>
<head>
<title>Query Builder Class in CodeIgniter 4</title>
</head>
<body>
<h3>Result</h3>
Latest Id:<?= $latestId ?>
<br>
Affected Rows: <?= $affectedRows ?>
</body>
</html>
Define Routes
Open Routes.php file in app/Config folder. Set default controller as below:
$routes->get('/', 'Demo::index');
$routes->get('/demo', 'Demo::index');
$routes->get('/demo/index', 'Demo::index');
$routes->post('/demo/create', 'Demo::create');
Structure of CodeIgniter 4 Project
Run Application
Access index action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/index
Output
Latest Id:32
Affected Rows: 1