Académique Documents
Professionnel Documents
Culture Documents
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>
5. After that click menu insert And entry values. idPerson not entry because
idPerson is Auto Increment and idPerson will automatic create.
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.
// $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> ||