Vous êtes sur la page 1sur 17

Introducing Php CI ( Code Igniter )

1. Install Xampp Application for php and database My Sql


2. After Install XAMPP run XAMPP Application. And start mySQL Database
and Apache Web Server.
3. Download source PHP CI in address http://codeigniter.com/ .
4. copy the code igniter files on local web server (XAMPP) . in folder htdocs
5. extract the code igniter and rename this folder. example : learnCi
6. Now , open your browser and run web page learnCi . Example : localhost/
learnCi or 127.0.0.1/learnCi

7. PHP CI is a web MVC (Model View Controller).


Model : Usually the model function content assist a person in database
management . Example insert, update, and delete data into database.
View : View is a set the display to the user. Or generate output to users
Controller : controller is the bridge between model and view. The

controller will receive all requests from users, after that go to the model.
Create first application with Code Igniter ( CI )
1. First we make controller, create file name hello.php. In the folders
\application\controller. Enter following code :
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
class hello extends CI_Controller{
function __construct(){
parent::__construct();
}
function you(){
$this->load->view('you_view');
}
}
?>
2. Next step make a view. Create file name youView.php. In the folders
\application\view. Enter the following code :
<!DOCTYPE html>
<html>
<head>
<title>My First CodeIgniter Practice</title>
</head>
<body>
<h1> HELLO CI Buddy...!</h1>
</body>
</html>
Setting Configuration routes :
1. open folder \application\config\routes.php. Update the code
default_controller :
$route['default_controller'] = "hello";
3. Now test the application in browser with address http://localhost/learnCI/
index.php/hello/you

4. Change function name you with index. Refresh you browser. what's
happening page not found. Change the address browser http://localhost/
learnCI/index.php/
function index() automatic first run in function controller if address
browser not declaration. example : localhost/learnCI.
5. Next Step in function _construct() . Update code :
function __construct(){
parent::__construct();
$this->name="Abir";
$this->color="red";
}
6. And In Under function index(). Enter this code :
function getYourName(){
$data['name']=$this->name;
$data['color']=$this->color;
$this->load->view('youView2',$data);
}
7. Create new view with name youView2. Enter this code : Enter following
code
<!DOCTYPE html>
<html>
<head>
<title>My First CodeIgniter Practice</title>
</head>
<body>
<h1 style="color:<?php echo $color; ?>"> Hello <?php echo
$name; ?>! </h1>
</body>
</html>

8. Now test the application in browser with address http://localhost/learnCI/


index.php/hello/getYourName/

9. Ok next step create function in hello controller with name getYourName2() .


Example :
function getYourName2(){
$data['name']=$_GET['name'];
$this->load->view('youView3',$data);
}
9. Create new view with name youView3. Enter this code :
<!DOCTYPE html>
<html>
<head>
<title>My First CodeIgniter Practice</title>
</head>
<body>
<h1> Hello <?php echo $name; ?>! </h1>
</body>
</html>
10. Now test the application in browser with address http://localhost/learnCI/
index.php/hello/getYourName2?name=arfian

Create project crud ( Create, read, update, delete ) with PHP CI

- Setting Base Url Configuration


open config.php within codeigniter\application\config. Set config like
below:
$config['base_url'] = 'http://localhost/learnCI/';
- Setting Configuration database
open database.php within codeigniter\application\config. Set config like
below:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = ci_test;
$db['default']['dbdriver'] = 'mysql';
- Preparing Database
1. Open phpmyadmin
2. Create Database with name ci_test
3. Click ci_test create table database with name person and number of
column 4.
4. Next Step. Entry the table configuration database :

5. After that click menu insert And entry values. idPerson not entry because
idPerson is Auto Increment and idPerson will automatic create.

6. Update code in folder \application\config\autoload.php :


$autoload['libraries'] = array('database','pagination');
$autoload['helper'] = array('form','url');
7. Create file php in folder mode with name personModel.php. and Enter
following code :
<?php
class personModel extends CI_Model {
public function get_data_person() {
$query = $this->db->get(person);
return $query->result();
}
}
8. Next Step create file php in folder controller with name
personController.php. and Enter following code :
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class personController extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->model('personModel');
$data['person']=$this->personModel->get_data_person();
$this->load->view('viewPerson',$data);
}
}
?>
9. Setting again Configuration routes :
open folder \application\config\routes.php. Update the code
default_controller :
$route['default_controller'] = personController;
10. Now Create file php in folder view with name viewPerson.php. and Enter

following code :
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>
</head>
<body>
<table align="left" border="1" cellspacing="0" cellpadding="0"
width="100%">
<tr align="left" valign="top">
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
</tr>
<?php $i=0; foreach($person as $row){ $i++; ?>
<tr align="left" valign="top">
<td><?php echo $i; ?></td>
<td><?php echo $row->idPerson; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->age; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
11. Ok now run in browser http://localhost/learnCI/ .

- Insert in database
1. Create file php in folder view with name insertPerson.php . and Enter
following code :
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>

</head>
<body>
<form name="insertPerson" method="post" action="<?php echo
site_url('personController/insertPerson') ?>" enctype="application/x-wwwform-urlencoded">
<fieldset>
<legend> Form Insert Person</legend>
Name :<br>
<input type="text" name="name" maxlength="50" /><hr>
Address :<br>
<input type="text" name="address" maxlength="100" /><hr>
Age : <br>
<input type="text" name="age" maxlength="10" /><hr/>
<input type="submit" value="Save" />
<input type="button" name="Cancel"
onclick="javascript:document.location.href='<?php echo site_url(); ?>'"
value="Batal" />
</fieldset>
</form>
</body>
</html>
2. Insert source code in personModel.php.
public function insertPerson($data){
$query = $this->db->insert('person', $data);
return $query;
}
3. insert function in person Controller. Enter following code :
function insertViewPerson()
{
$this->load->view('insertPerson');
}
function insertPerson()
{
$data=array(
'name'=>$_POST['name'],
'address'=>$_POST['address'],
'age'=>$_POST['age'],
);
$this->load->model('personModel');
$res=$this->personModel->insertPerson($data);
if($res){

redirect(site_url());
}else{
echo "Fail insert Data <br>";
echo '<a href="'.site_url('personController/
insertViewPerson').'">Back</a>';
}
}
4. Update source code in viewPerson.php :
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>
</head>
<body>
<a href="<?php echo site_url("personController/insertViewPerson"); ?
>">Insert Person</a>
<table align="left" border="1" cellspacing="0" cellpadding="0"
width="100%">
<tr align="left" valign="top">
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
</tr>
<?php $i=0; foreach($person as $row){ $i++; ?>
<tr align="left" valign="top">
<td><?php echo $i; ?></td>
<td><?php echo $row->idPerson; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->age; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
5. Refresh your browser And click Insert Person and insert this form. After that
click save.

- Delete person database :


1. Update source code in viewPerson.php :
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>
</head>
<body>
<a href="<?php echo site_url("personController/insertViewPerson"); ?
>">Insert Person</a>
<table align="left" border="1" cellspacing="0" cellpadding="0"
width="100%">
<tr align="left" valign="top">
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
<th>Action</th>
</tr>
<?php $i=0; foreach($person as $row){ $i++; ?>
<tr align="left" valign="top">
<td><?php echo $i; ?></td>

<td><?php echo $row->idPerson; ?></td>


<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->age; ?></td>
<td>
<a href="<?php echo site_url('personController/
deletePerson?idPerson='.$row->idPerson)?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
2. After that , Insert source code in personModel.php.
public function deletePerson($data){
$query=$this->db->delete('person',$data);
return $query;
}
3. insert function in person Controller. Enter following code :
function deletePerson(){
$data=array(
'idPerson'=>$_GET['idPerson']
);
$this->load->model('personModel');
$res=$this->personModel->deletePersonModel($data);
if($res){
redirect(site_url());
}else{
echo "Fail delete Data <br>";
echo '<a href="'.site_url().'">Back</a>';
}
}
4. Now Refresh your browser And Click delete.

- Update Person Data :


1. Create new file in view with name updatePerson.php . The source almost
equal with insertPerson.php . Enter following code :
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>
</head>
<body>
<form name="updatePerson" method="post" action="<?php echo
site_url('personController/updatePerson') ?>" enctype="application/x-wwwform-urlencoded">
<fieldset>
<legend> Form Update Person</legend>
<input type="hidden" name="idPerson" value="<?php echo
$person->idPerson ?>" />
Name :<br>
<input type="text" name="name" value="<?php echo $person>name ?>" maxlength="50" /><hr>
Address :<br>
<input type="text" name="address" value="<?php echo
$person->address ?>" maxlength="100" /><hr>
Age : <br>
<input type="text" name="age" value="<?php echo $person>age ?>" maxlength="10" /><hr/>
<input type="submit" value="Update" />
<input type="button" name="Cancel"
onclick="javascript:document.location.href='<?php echo site_url(); ?>'"
value="Batal" />
</fieldset>
</form>
</body>
</html>
2. Insert function in personModel.php. Enter following code :
public function getDataPersonId($data){
$this->db->where($data);
$query=$this->db->get('person');
$data=$query->first_row();
return $data;
}

public function updatePersonModel($data){


$this->db->where('idPerson',$data['idPerson']);
$query=$this->db->update('person',$data);
return $query;
}
3. Insert function in personController.php.
function updateViewPerson()
{
$param=array('idPerson'=>$_GET['idPerson']);
$this->load->model('personModel');
$data['person']=$this->personModel->getDataPersonId($param);
$this->load->view('updatePerson',$data);
}
function updatePerson()
{
$data=array(
'idPerson'=>$_POST['idPerson'],
'name'=>$_POST['name'],
'address'=>$_POST['address'],
'age'=>$_POST['age'],
);
$this->load->model('personModel');
$res=$this->personModel->updatePersonModel($data);
if($res){
redirect(site_url());
}else{
echo "Fail update Data <br>";
echo '<a href="'.site_url('personController/index').'">Back</a>';
}
}
4. After that, update source code viewPerson.php . just added action <a
href="<?php echo site_url('personController/updateViewPerson?idPerson='.
$row->idPerson)?>">Update</a> in <td>
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>
</head>
<body>

<a href="<?php echo site_url("personController/insertViewPerson"); ?


>">Insert Person</a>
<table align="left" border="1" cellspacing="0" cellpadding="0"
width="100%">
<tr align="left" valign="top">
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
<th>Action</th>
</tr>
<?php $i=0; foreach($person as $row){ $i++; ?>
<tr align="left" valign="top">
<td><?php echo $i; ?></td>
<td><?php echo $row->idPerson; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->age; ?></td>
<td>
<a href="<?php echo site_url('personController/
updateViewPerson?idPerson='.$row->idPerson)?>">Update</a> ||
<a href="<?php echo site_url('personController/
deletePerson?idPerson='.$row->idPerson)?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
5. Refresh your browser address http://localhost/learnCI/index.php . click
update.

- Paging data person


1. Create new function in personController . Enter following code :
function viewPersonPaging($id=NULL) {
$this->load->model('personModel');
$amount = $this->personModel->getAmountPerson();
//configure pagination
$config['base_url'] = base_url() . 'index.php/personController/
viewPersonPaging';
$config['total_rows'] = $amount->num_rows();
$config['per_page'] = '2';
$config['first_page'] = 'First';
$config['last_page'] = 'Last';
$config['next_page'] = '&laquo;';
$config['prev_page'] = '&raquo;';
//inisialisasi config
$this->pagination->initialize($config);
//create pagination
$data['pagePerson'] = $this->pagination->create_links();
//view data
$data['person'] = $this->personModel>get_data_personPaging($config['per_page'], $id);
$this->load->view('viewPerson', $data);
}
2. Command code in index n call function viewPersonPaging .
function index(){
// $this->load->model('personModel');
// $data['person']=$this->personModel->get_data_person();

// $this->load->view('viewPerson',$data);
$this->viewPersonPaging();
}
3. Create new function in personModel . Enter following code :
public function get_data_personPaging($num, $offset) {
$query = $this->db->get('person', $num, $offset);
return $query->result();
}
public function getAmountPerson(){
return $this->db->get('person');
}
4. After that, update source code viewPerson.php in bottom table : <div
height="30px" width="100%"> <?php echo $pagePerson; ?> </div>
<!DOCTYPE html>
<html>
<head>
<title>CRUD Database</title>
</head>
<body>
<a href="<?php echo site_url("personController/insertViewPerson"); ?
>">Insert Person</a>
<table align="left" border="1" cellspacing="0" cellpadding="0"
width="100%">
<tr align="left" valign="top">
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
<th>Action</th>
</tr>
<?php $i=0; foreach($person as $row){ $i++; ?>
<tr align="left" valign="top">
<td><?php echo $i; ?></td>
<td><?php echo $row->idPerson; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->address; ?></td>
<td><?php echo $row->age; ?></td>
<td>
<a href="<?php echo site_url('personController/
updateViewPerson?idPerson='.$row->idPerson)?>">Update</a> ||

<a href="<?php echo site_url('personController/


deletePerson?idPerson='.$row->idPerson)?>">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<div height="30px" width="100%"> <?php echo $pagePerson; ?> </div>
</body>
</html>
5. now refresh browser http://localhost/learnCI/index.php/ .

Vous aimerez peut-être aussi