As you have seen what is model, controller, and view now time to work practically connect each of them and fetch the data from the database
Database Structure
create a database and connect to your application/config/database.php file following code will help you to create the database:
-- phpMyAdmin SQL Dump
-- version 5.0.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 02, 2021 at 09:23 AM
-- Server version: 10.4.17-MariaDB
-- PHP Version: 7.3.25
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `tutorial`
--
-- --------------------------------------------------------
--
-- Table structure for table `customer`
--
CREATE TABLE `customer` (
`id` int(11) NOT NULL,
`name` varchar(32) NOT NULL,
`city` varchar(32) NOT NULL,
`Country` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `customer`
--
INSERT INTO `customer` (`id`, `name`, `city`, `Country`) VALUES
(1, 'Rathorji', 'Mumbai', 'India'),
(2, 'Jhone', 'Washington, D.C.', 'USA'),
(3, 'Amit', 'Delhi', 'India'),
(4, 'Rohit', 'Indore', 'India');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `customer`
--
ALTER TABLE `customer`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Model
application/models/CustomerModel.php
<?php
class CustomerModel extends CI_Model {
public function get_customer() {
$this->load->database();
$query = $this->db->query("SELECT * FROM `customer`");
return $query->result_array();
}
}
controller
application/controllers/DataCtrl.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class DataCtrl extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('CustomerModel');
}
public function index() {
$data = array(
"customers" => $this->CustomerModel->get_customer()
);
$this->load->view('data' ,$data);
}
}
Views
application/views/data.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Create a model, view, controller, and fetch data from the database</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div id="container">
<h1>Customers!</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<?php foreach ($customers as $key => $customer):?>
<tr>
<td><?php echo $customer['name']?></td>
<td><?php echo $customer['city']?></td>
<td><?php echo $customer['Country']?></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
</div>
</body>
</html>
Run the application here my project name is "bcit-ci-CodeIgniter-b73eb19" i am going through this URL and look the result:
http://localhost/bcit-ci-CodeIgniter-b73eb19/DataCtrl
output:
index.php file removed because i have put this following code in .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]