Intro To Code Igniter
Intro To Code Igniter
CodeIgniter
Prerequisite
Is a Software framework
Designed to support the development of
Dynamic websites
Web applications
Web services
Figure : 01
What is CodeIgniter ???
An Open Source Web Application Framework
Nearly Zero Configuration
MVC ( Model View Controller ) Architecture
Multiple DB (Database) support
DB Objects
Templating
Caching
Modules
Validation
Rich Sets of Libraries for Commonly Needed Tasks
Has a Clear, Thorough documentation
Installation of CodeIgniter
Requirments
Web Server - Download & Install Apache
PHP – 4.3.2 or Higher
Database – MySQL ( support for other DB exists )
Installation
Download the latest version from www.codeigniter.com and unzip into your web
root directory.
Open “application/config/config.php” and change base_url value to base url. For
example: http://localhost/myci/
To Use Database open “application/config/database.php” and change necessary
values. Usually you have to change: ‘hostname’, ‘username’, ‘password’, ‘datbase’.
Start Your Web Server and Database Server and go to http://localhot/myci
Application Flow Of
CodeIgniter
Segments in a URI
www.your-site.com/class/function/ID
www.your-
site.com/index.php?c=news&m=article&ID=345
Controllers
A Class file resides under “application/controllers”
www.your-site.com/index.php/first
<?php
function First() {
parent::Controller();
}
function index() {
echo “<h1> Hello CUET !! </h1> “;
}
}
?>
• Note:
• Class names must start with an Uppercase Letter.
• In case of “constructor” you must use “parent::Controller();”
Controllers
In This Particular Code
www.your-site.com/index.php/first/bdosdn/world
<?php
function index() {
echo “<h1> Hello CUET !! </h1> “;
}
function bdosdn( $location ) {
echo “<h2> Hello $location !! </h2>”;
}
}
?>
// Output Will be “Hello world !!”
• Note:
• The ‘Index’ Function always loads by default. Unless there is a second
segment in the URL
VIEWS
A Webpage or A page Fragment
Should be placed under “application/views”
Never Called Directly
web_root/myci/system/application/views/myview.php
<html>
<title> My First CodeIgniter Project</title>
<body>
<h1> Welcome ALL … To My .. ::: First Project ::: .
. . </h1>
</body>
</html>
16
VIEWS
Calling a VIEW from Controller
$this->load->view(‘myview’);
function index() {
$var = array(
‘full_name’ => ‘Amzad Hossain’,
‘email’ => ‘tohi1000@yahoo.com’
);
$this->load->view(‘myview’, $var);
}
<html>
<title> ..::Personal Info::.. </title>
<body>
Full Name : <?php echo $full_name;?> <br />
E-mail : <?=email;?> <br />
</body>
</html>
VIEWS
There are 3 mechanism that can be utilize to show Dynamic Data inside a
VIEW File
- Pure PHP
- PHP’s Alternative Syntax
- CodeIgniter’s Template Engine
• Note:
• There are other alternative syntax ‘for’, ‘foreach’, ‘while’
Models
Designed to work with Information of Database
Models Should be placed Under “application/models/”
<?php
$this->load->model(‘mymodel’);
$data = $this->mymodel->get_info();
CodeIgniter Libraries
Special Purpose Classes
General Approach
function index() {
$this->load->library(‘database’);
$rslt = $this->db->query(“select first_name from user_name”);
foreach( $rslt->result() as $row_data)
echo $row_data->first_name . “<br />”;
$this->load->library(‘database’);
$this->db->select(“first_name”);
$rslt = $this->db->get(“user_name”);
foreach( $rslt->result() as $row_data)
echo $row_data->first_name . “<br />”;
}
Helpers
Simply a collection of functions in a particular category.
Array Date File HTML Smiley Text
$this->load->helper(array(‘form’,’url’) );
Helpers
Form Helper
form_open()
form_open_multipart()
form_input()
form_textarea()
form_checkbox()
form_submit()
form_close()
URL Helper
site_url()
base_url()
anchor()
anchor_popup()
mailto()
A Personal Blog Using CI
Questions???