0% found this document useful (0 votes)
93 views37 pages

Codeigniter Unit-3

The document discusses Create, Read, Update and Delete (CRUD) operations using PHP CodeIgniter framework. It covers configuring CodeIgniter files and database, working with models, controllers and views to perform CRUD operations. Specifically, it shows how to create a database table, insert a record into the table using a model, and display the inserted data on a view page using a controller.

Uploaded by

Harsh Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views37 pages

Codeigniter Unit-3

The document discusses Create, Read, Update and Delete (CRUD) operations using PHP CodeIgniter framework. It covers configuring CodeIgniter files and database, working with models, controllers and views to perform CRUD operations. Specifically, it shows how to create a database table, insert a record into the table using a model, and display the inserted data on a view page using a controller.

Uploaded by

Harsh Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Asst. Prof.

Bharat Sandanshiv

Unit 3 :-
Create,Read,Update & Delete with using PHP
Codeigniter

1
Asst. Prof. Bharat Sandanshiv

Unit 3 :- Create,Read,Update & Delete with using


PHP Codeigniter

• Configure basic files of PHP Codeigniter Folder

• Creating Database

• Working With Model

• Working With Controllers

• Working With Views


2
Asst. Prof. Bharat Sandanshiv

 Configure basic files of PHP Codeigniter Folder


 Creating Database
• In CodeIgniter, go to application/config/databse.php for database
configuration file.

3
Asst. Prof. Bharat Sandanshiv

• In database.php file, fill the entries to connect CodeIgniter folder to your


database.

4
Asst. Prof. Bharat Sandanshiv

Working With Views


• View folder contains all the markup files header, footer, sidebar,
etc.

• They can be reused by embedding anywhere in the controller.

• They are the interface design which is displayed on the user's


browser and can never be called directly, they have to be loaded
in the controller's file.

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.

• It is always loaded in the controller file.

• The following line is used to load a view page.

7
Asst. Prof. Bharat Sandanshiv

• Write your view's page name in the bracket.


• You don't need to specify .php unless you are using some other
extension.
• Now, go to your controller file (Main.php) and write this code as
shown below.

8
Asst. Prof. Bharat Sandanshiv

Loading Multiple Views


• Your view may contain several files like header, footer, side menu,
form, etc.
• Sometimes you may need to load more than one file
simultaneously.
• In that case, simply call $this->load->view() multiple times.

9
Asst. Prof. Bharat Sandanshiv

Working With Controllers


• A controller is the intermediary between models and views to
process HTTP request and generates a web page.

• All the requests received by the controller are passed on to


models and views to process the information.

• It is the center of every request on your web application.

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

Working With Model


• In any application you need to call a function to retrieve some
information from the database.

• Models responsibility is to handle all data logic and representation


and load data in the views.

• It is stored in application/models.

12
Asst. Prof. Bharat Sandanshiv

 CodeIgniter First Example


 Create file in Views
• Create a file named "hello_world.php".
• Save this file in the View folder of your CodeIgniter.
• Write the following coding.

<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>Hello World!!</p>
</body>
</html>
13
Asst. Prof. Bharat Sandanshiv

 Create file in Controllers


• In Controller create a file named "Hello.php".
• This file will be saved in the Controller folder of your CodeIgniter.
• Write the following coding

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Hello extends CI_Controller {

public function index()


{
$this->load->view('hello_world');
}
}
?>
14
Asst. Prof. Bharat Sandanshiv

 Run the Controller file

15
Asst. Prof. Bharat Sandanshiv

 CodeIgniter Select Record


 Fetching data from table.
• Step 1 :- Create a database and set database name in config folder open
database.php file

16
Asst. Prof. Bharat Sandanshiv

• Create file in views Folder -> student_regi_form.php

17
Asst. Prof. Bharat Sandanshiv

• Create file in model Folder -> mlp_college_model.php


• Here we can write class name and file name is a same.
• But class name first character is a Capital case.

18
Asst. Prof. Bharat Sandanshiv

• Create file in controllers Folder -> mlp_college.php


• Here we can write class name and file name is a same.
• But class name first character is a Capital case.

19
Asst. Prof. Bharat Sandanshiv

 Output

20
Asst. Prof. Bharat Sandanshiv

 Insert Record Using CodeIgniter


 Step-1 Create Databse with table

 Step-2 Connecting to a Databse


1) Automatic Connecting
go to the application/config/autoload.php

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 database libray manually


$this->load->database();

//load Model
$this->load->model('Hello_Model');
}
22
Asst. Prof. Bharat Sandanshiv
public function savedata()
{
//load registration view form
$this->load->view('registration');

//Check submit button


if($this->input->post('save'))
{
//get form's data and store in local varable
$n=$this->input->post('name');
$e=$this->input->post('email');
$m=$this->input->post('mobile');

//call saverecords method of Hello_Model and pass variables as parameter


$this->Hello_Model->saverecords($n,$e,$m);
echo "Records Saved Successfully";
}
}
}
?>

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

• Calling/run Hello Controller’s savedata method

• Open your web browser and pass:

• http://localhost/college_management_system/index.php/hello/savedata

 Output

27
Asst. Prof. Bharat Sandanshiv

 Display , Delete and Update Record Using CodeIgniter


 Creating a Controller
 Create a new file under the path
Application/controllers/Hello.php

<?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

public function savedata()


{
$this->load->view('registration');
if($this->input->post('save'))
{
$n=$this->input->post('name');
$e=$this->input->post('email');
$m=$this->input->post('mobile');
$this->Hello_Model->saverecords($n,$e,$m);
redirect("Hello/dispdata");
}
}

public function dispdata()


{
$result['data']=$this->Hello_Model->displayrecords();
$this->load->view('display_records',$result);
}
29
Asst. Prof. Bharat Sandanshiv
public function deletedata()
{
$id=$this->input->get('id');
$this->Hello_Model->deleterecords($id);
redirect("Hello/dispdata");
}

public function updatedata()


{
$id=$this->input->get('id');
$result['data']=$this->Hello_Model->displayrecordsById($id);
$this->load->view('update_records',$result);

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

• Calling/run Hello Controller’s savedata method

• Open your web browser and pass:

• http://localhost/college_management_system/index.php/Hello/dispdata

• Output

37

You might also like