Create Database
Create new database named learn_codeigniter_with_real_apps. 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,
`description` text NOT NULL,
`status` tinyint(1) NOT NULL,
`photo` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `status`, `photo) VALUES('name 1', 2, 5, 'Description 1', 0, 'thumb1.gif');
INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `status`, `photo) VALUES('name 2', 7, 3, 'Description 2', 1, 'thumb2.gif');
INSERT INTO `product` (`name`, `price`, `quantity`, `description`, `status`, `photo) VALUES('name 3', 2, 7, 'Description 3', 1, 'thumb3.gif');
Product Table
Data of Product Table
Create Assets Folder
Create new folder named assets in root project. In this folder, create new folders as below:
Images Folder
Create new folder named images in assets folder. Copy images need to use in project to images folder.
Set BASE URL
In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:
$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';
AutoLoad Libraries
In CodeIgniter Project, Open autoload.php file in config folder. Add database and table libraries to libraries config as below:
$autoload['libraries'] = array('database', 'table');
Connect to Database
Open database.php file in config folder. Add values as below connect to database:
$db['default'] = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '123456',
'database' => 'learn_codeigniter_with_real_apps'
);
Create Product Model
Create new PHP file named product_model.php in models folder as below:
<?php
if (! defined('BASEPATH'))
exit('No direct script access allowed');
class Product_model extends CI_Model
{
function findAll()
{
return $this->db->get('product')->result();
}
}
AutoLoad Product Model
In CodeIgniter Project, Open autoload.php file in config folder and set value for model config as below:
$autoload['model'] = array('product_model' => 'productModel');
Create Controller
Create new PHP file named product.php in controllers folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Product extends CI_Controller
{
public function index()
{
$data['products'] = $this->productModel->findAll();
$this->load->view('product/index', $data);
}
}
Set Default Controller
Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'product';
Create View
Create new folder named product in views folder. In this folder, create new PHP file named index.php as below:
<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
<head>
<title>Product List</title>
</head>
<body>
<h3>Product List</h3>
<?php
$tmpl = array (
'table_open' => '<table border="1" cellpadding="2" cellspacing="2">'
);
$this->table->set_template($tmpl);
$this->table->set_heading('ID', 'Name', 'Photo', 'Status', 'Description', 'Price', 'Quantity', 'Sub Total');
foreach ($products as $product) {
$this->table->add_row(
$product->id,
$product->name,
'<img src="'.base_url().'assets/images/'.$product->photo.'" height="50">',
$product->status ? 'active' : 'inactive',
$product->description,
$product->price,
$product->quantity,
$product->price * $product->quantity
);
}
echo $this->table->generate();
?>
</body>
</html>
Structure of CodeIgniter Project
Run Application
Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/product/index
Output
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- CodeIgniter Web Application Blueprints
- CodeIgniter for Rapid PHP Application Development: Improve your PHP coding productivity with the free compact open-source MVC CodeIgniter framework!
- Programming with CodeIgniter MVC
- Murach’s PHP and MySQL (3rd Edition)
- Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
- PHP and MySQL Web Development (5th Edition) (Developer’s Library)