Codeigniter Unit-3
Codeigniter Unit-3
Bharat Sandanshiv
Unit 3 :-
Create,Read,Update & Delete with using PHP
Codeigniter
1
Asst. Prof. Bharat Sandanshiv
• Creating Database
3
Asst. Prof. Bharat Sandanshiv
4
Asst. Prof. Bharat Sandanshiv
5
Asst. Prof. Bharat Sandanshiv
View syntax
• Create a file and save it in application/views folder.
• For example, we have created a file Firstview.php
6
Asst. Prof. Bharat Sandanshiv
Loading View
• View can't be accessed directly.
7
Asst. Prof. Bharat Sandanshiv
8
Asst. Prof. Bharat Sandanshiv
9
Asst. Prof. Bharat Sandanshiv
10
Asst. Prof. Bharat Sandanshiv
Controller Syntax
• Look at the above, controller's file name is Main.php (first letter has to be in
uppercase) and class name is Main (first letter has to be in uppercase).
11
Asst. Prof. Bharat Sandanshiv
• It is stored in application/models.
12
Asst. Prof. Bharat Sandanshiv
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>Hello World!!</p>
</body>
</html>
13
Asst. Prof. Bharat Sandanshiv
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
15
Asst. Prof. Bharat Sandanshiv
16
Asst. Prof. Bharat Sandanshiv
17
Asst. Prof. Bharat Sandanshiv
18
Asst. Prof. Bharat Sandanshiv
19
Asst. Prof. Bharat Sandanshiv
Output
20
Asst. Prof. Bharat Sandanshiv
2) Manual Connecting
21
Asst. Prof. Bharat Sandanshiv
Creating a Controller
Create a new file under the path
Application/controllers/Hello.php
<?php
class Hello extends CI_Controller
{
public function __construct()
{
//call CodeIgniter's default Constructor
parent::__construct();
//load Model
$this->load->model('Hello_Model');
}
22
Asst. Prof. Bharat Sandanshiv
public function savedata()
{
//load registration view form
$this->load->view('registration');
23
Asst. Prof. Bharat Sandanshiv
Creating a View
Create a new file under the path
Application/views/registration.php
<!DOCTYPE html>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Enter Your Name </td>
<td width="329"><input type="text" name="name"/></td>
</tr> 24
Asst. Prof. Bharat Sandanshiv
<tr>
<td>Enter Your Email </td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>Enter Your Mobile </td>
<td><input type="text" name="mobile"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="save" value="Save
Data"/></td>
</tr>
</table>
</form>
</body>
</html>
25
Asst. Prof. Bharat Sandanshiv
Creating a Model
Create a new file under the path
Application/models/hello_model.php
<?php
class Hello_Model extends CI_Model
{
function saverecords($name,$email,$mobile)
{
$query="insert into users values('','$name','$email','$mobile')";
$this->db->query($query);
}
}
26
Asst. Prof. Bharat Sandanshiv
• http://localhost/college_management_system/index.php/hello/savedata
Output
27
Asst. Prof. Bharat Sandanshiv
<?php
class Hello extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->model('Hello_Model');
}
28
Asst. Prof. Bharat Sandanshiv
if($this->input->post('update'))
{
$n=$this->input->post('name');
$e=$this->input->post('email');
$m=$this->input->post('mobile');
$this->Hello_Model->updaterecords($n,$e,$m,$id);
redirect("Hello/dispdata");
}
}
}
?>
30
Asst. Prof. Bharat Sandanshiv
Creating a View
Create a new file under the path
Application/views/display_records.php
<!DOCTYPE html>
<html>
<head>
<title>Display Records</title>
</head>
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr style="background:#CCC">
<th>Sr No</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Delete</th>
<th>Update</th>
</tr>
31
Asst. Prof. Bharat Sandanshiv
<?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->name."</td>";
echo "<td>".$row->email."</td>";
echo "<td>".$row->mobile."</td>";
echo "<td><a href='deletedata?id=".$row->user_id."'>Delete</a></td>";
echo "<td><a href='updatedata?id=".$row->user_id."'>Update</a></td>";
echo "</tr>";
$i++;
}
?>
</table>
</body>
</html>
32
Asst. Prof. Bharat Sandanshiv
Creating a View
Create a new file under the path
Application/views/update_records.php
<!DOCTYPE html>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Enter Your Name </td>
<td width="329"><input type="text" name="name" value="<?php echo $row-
>name; ?>"/> </td> </tr> 33
Asst. Prof. Bharat Sandanshiv
<tr>
<td>Enter Your Email </td>
<td><input type="text" name="email" value="<?php echo $row->email;
?>"/></td>
</tr>
<tr>
<td>Enter Your Mobile </td>
<td><input type="text" name="mobile" value="<?php echo $row->mobile;
?>"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="update" value="Update Records"/></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html> 34
Asst. Prof. Bharat Sandanshiv
Creating a Model
Create a new file under the path
Application/models/hello_model.php
<?php
class Hello_Model extends CI_Model
{
function saverecords($name,$email,$mobile)
{
$query="insert into users values('','$name','$email','$mobile')";
$this->db->query($query);
}
function displayrecords()
{
$query=$this->db->query("select * from users");
return $query->result();
} 35
Asst. Prof. Bharat Sandanshiv
function deleterecords($id)
{
$this->db->query("delete from users where user_id='".$id."'");
}
function displayrecordsById($id)
{
$query=$this->db->query("select * from users where user_id='".$id."'");
return $query->result();
}
function updaterecords($name,$email,$mobile,$id)
{
$query=$this->db->query("update users SET
name='$name',email='$email',mobile='$mobile' where user_id='".$id."'");
}
}
?>
36
Asst. Prof. Bharat Sandanshiv
• http://localhost/college_management_system/index.php/Hello/dispdata
• Output
37