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/';
Create Product Entity
Create new PHP file named product.php in models folder as below:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Product
{
var $id;
var $name;
var $price;
function __construct($id, $name, $price)
{
$this->id = $id;
$this->name = $name;
$this->price = $price;
}
}
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');
require 'product.php';
class Product_model extends CI_Model
{
function find()
{
return new Product('p01', 'name 1', 2, 3, 'thumb1.gif');
}
function findAll()
{
$product1 = new Product('p01', 'name 1', 2);
$product2 = new Product('p02', 'name 2', 7);
$product3 = new Product('p03', 'name 3', 5);
$products = array($product1, $product2, $product3);
return $products;
}
}
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');
AutoLoad Libraries
In CodeIgniter Project, Open autoload.php file in config folder. Add session library to libraries config as below:
$autoload['libraries'] = array('session');
Create Controller
Create new PHP file named demo.php in controllers folder as below:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Demo extends CI_Controller
{
public function index()
{
/* Session Data */
$this->session->set_userdata('age', 10);
$this->session->set_userdata('username', 'PMKLab');
/* Session Array Data */
$account = array(
'username' => 'pmk',
'fullName' => 'PMK Lab'
);
$this->session->set_userdata('account', $account);
/* Session Object Data */
$this->session->set_userdata('product', serialize($this->productModel->find()));
/* Session Object List Data */
$this->session->set_userdata('products', serialize($this->productModel->findAll()));
$this->load->view('demo/index');
}
}
Set Default Controller
In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:
$route['default_controller'] = 'demo';
Create View
Create new folder named demo 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>Demo Page</title>
</head>
<body>
<h3>Session Data</h3>
Age: <?php echo $this->session->userdata('age'); ?>
<br>
Username: <?php echo $this->session->userdata('username'); ?>
<h3>Session Array Data</h3>
<?php
$account = $this->session->userdata('account');
?>
Username: <?php echo $account['username']; ?>
<br>
Full Name: <?php echo $account['fullName']; ?>
<h3>Session Object Data</h3>
<?php
$product = unserialize($this->session->userdata('product'));
?>
Id: <?php echo $product->id; ?>
<br>
Name: <?php echo $product->name; ?>
<br>
Price: <?php echo $product->price; ?>
<h3>Session Object List Data</h3>
<?php
$products = unserialize($this->session->userdata('products'));
?>
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
</tr>
<?php foreach($products as $product) { ?>
<tr>
<td><?php echo $product->id; ?></td>
<td><?php echo $product->name; ?></td>
<td><?php echo $product->price; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Structure of CodeIgniter Project
Run Application
Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/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)