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();
}
public function demo1()
{
$data['products'] = $this->db->table($this->table)->limit(2)->get()->getResult();
return view('demo/index', $data);
}
public function demo2()
{
$data['products'] = $this->db->table($this->table)->limit(3, 1)->get()->getResult();
return view('demo/index', $data);
}
public function demo3()
{
$data['products'] = $this->db->table($this->table)->orderBy('price', 'desc')->limit(2)->get()->getResult();
return view('demo/index', $data);
}
public function demo4()
{
$data['products'] = $this->db->table($this->table)->where(["status" => 1])->orderBy('price', 'asc')->limit(2)->get()->getResult();
return view('demo/index', $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>Product List</h3>
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<th>Id</th>
<th>Name</th>
<th>Status</th>
<th>Created</th>
<th>Price</th>
<th>Quantity</th>
<th>Description</th>
</tr>
<?php foreach ($products as $product) { ?>
<tr>
<td><?= $product->id ?></td>
<td><?= $product->name ?></td>
<td><?= $product->status ?></td>
<td><?= $product->created ?></td>
<td><?= $product->price ?></td>
<td><?= $product->quantity ?></td>
<td><?= $product->description ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Define Routes
Open Routes.php file in app/Config folder. Set default controller as below:
$routes->get('/', 'Demo::demo1');
$routes->get('/demo', 'Demo::demo1');
$routes->get('/demo/demo1', 'Demo::demo1');
$routes->get('/demo/demo2', 'Demo::demo2');
$routes->get('/demo/demo3', 'Demo::demo3');
$routes->get('/demo/demo4', 'Demo::demo4');
Structure of CodeIgniter 4 Project
Run Application
Access demo1 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/demo1
Output
Access demo2 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/demo2
Output
Access demo3 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/demo3
Output
Access demo4 action in Demo controller with following url: http://localhost:8095/LearnCodeIgniter4WithRealApps/demo/demo4
Output