Blog Details

ADD-Update-Delete Operation In Codeigniter


                                
                                    <h2><strong>* Creating First Application at CodeIgniter:-</strong></h2>
<strong>1)</strong>  First, we make controller, create a file name "pages.php"   Create a file    at <strong><dfn>application/controllers/main.php</dfn></strong> with the following code.

&lt;?php
class main extends CI_Controller
{
<strong>public function __construct()</strong>
{
parent::__construct();
$this-&gt;load-&gt;helper('url');
$this-&gt;load-&gt;model('main_model');
}
<strong>public function display()</strong>
{
$data['res']=$this-&gt;main_model-&gt;get_data();
$this-&gt;load-&gt;view('main_view',$data);
}
<strong>public function operation()</strong>
{

if(isset($_POST['btn']))

{

if(empty($_POST['id']))
{
$this-&gt;main_model-&gt;add_data($_POST);
}
else
{

$this-&gt;main_model-&gt;edit_data($_POST);
}
$this-&gt;display();
}

else

{

$id=$this-&gt;input-&gt;get('id');
if($id)
{
$data['value']=$this-&gt;main_model-&gt;get_data_id($this-&gt;input-&gt;get('id'));
$this-&gt;load-&gt;view('form1',$data);
}
else
{
$this-&gt;load-&gt;helper('form');
$this-&gt;load-&gt;library('form_validation');
$this-&gt;load-&gt;view('form1');
}}}

<strong>public function delete($id)</strong>
{
$delete=$this-&gt;main_model-&gt;delete_data($id);
$this-&gt;display()

}

<strong>public function index()</strong>
{
$this-&gt;load-&gt;view('form1');
}

}

?&gt;

<strong>2)</strong>  First, we make model, create a file name "main_model.php"   Create a file    at <strong><dfn>application/model/main_model.php</dfn></strong> with the following code.

&lt;?php

class main_model extends CI_Model
{
<strong>public function __construct()</strong>
{
parent::__construct();
$this-&gt;load-&gt;database();
}

<strong>function get_data()</strong>
{
$query = $this-&gt;db-&gt;get('register');
return $query;
}
<strong>function get_data_id($id)</strong>
{
$query = $this-&gt;db-&gt;get_where('register',array('id' =&gt; $id),1);
return $query;
}
<strong>function delete_data($id)</strong>
{
$this-&gt;db-&gt;delete('register',array('id'=&gt;$id));
}
<strong>function add_data($data)</strong>
{
$value=array('username'=&gt;$data['txt_user'],'first_name'=&gt;$data['txt_fname'],

'last_name'=&gt;$data['txt_lname'],'gender'=&gt;$data['gender'],'mno'=&gt;$data['txt_mno']);

$this-&gt;db-&gt;insert('register',$value);
}

<strong>function edit_data($data)</strong>

<strong></strong>{

$value=array('username'=&gt;$data['txt_user'],'first_name'=&gt;$data['txt_fname'],

'last_name'=&gt;$data['txt_lname'],'gender'=&gt;$data['gender'],'mno'=&gt;$data['txt_mno']);

$this-&gt;db-&gt;update('register',$value,array('id' =&gt; $data['id']));

}}

?&gt;

<strong>3) </strong>Create the form at <strong><dfn>application/views/form1.php</dfn></strong> and add the following code.

&lt;?php

$username='';
$fname='';
$lname='';
$mno='';
$id='';
$submit='Add User';

if(isset($value) &amp;&amp; !empty($value))
{
foreach($value-&gt;result() as $row)
{
$username=$row-&gt;username;
$fname=$row-&gt;first_name;
$lname=$row-&gt;last_name;
$mno=$row-&gt;mno;
$id=$row-&gt;id;
$submit='Edit User';

i f($row-&gt;gender=='Male')
{?&gt;
&lt;script&gt; document.getElementById('gender_male').checked=true&lt;/script&gt;
&lt;?php

}

else
{ ?&gt;
&lt;script&gt; document.getElementById('gender_female').checked=true &lt;/script&gt;
&lt;?php }
}
}

?&gt;

&lt;html&gt;

&lt;head&gt;

&lt;script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"&gt;&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;form name="ajaxform" id="ajaxform" action="&lt;?php echo base_url() ?&gt;/index.php/main/operation" method="POST"&gt;

&lt;table border="1"&gt;

&lt;tr&gt;
&lt;th&gt;UserName&lt;/th&gt;
&lt;td&gt;&lt;input type="text" name="txt_user" value="&lt;?php echo $username; ?&gt; "                              /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;First Name&lt;/th&gt;
&lt;td&gt;&lt;input type="text" name="txt_fname" value="&lt;?php echo $fname; ?&gt; "                                  /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;Last Name&lt;/th&gt;
&lt;td&gt;&lt;input type="text" name="txt_lname" value="&lt;?php echo $lname; ?&gt; "                                  class="required"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th&gt;Gender&lt;/th&gt;
&lt;td&gt;
&lt;input type="radio" id="gender_male" name="gender" value="Male"/&gt;Male
&lt;input type="radio" id="gender_female" name="gender" value="Female"/&gt;Female
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;th&gt;Mobile No&lt;/th&gt;
&lt;td&gt;&lt;input type="text" name="txt_mno" value="&lt;?php echo $mno; ?&gt; "                                          /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;input type="hidden" name="id" value="&lt;?php echo $id; ?&gt;" /&gt;
&lt;input type="hidden" id="btn" value="&lt;?php echo $submit; ?&gt;" name="btn"/&gt;
&lt;input type="submit" id="simple-post" value="&lt;?php echo $submit; ?&gt;"                                         name="simple-post"/&gt;
&lt;/form&gt;

&lt;/body&gt;<a href="http://localhost/wordpress/wp-content/uploads/2014/04/My-View.html">
</a>

&lt;/html&gt;

<strong>4) </strong>Create the form at <strong><dfn>application/views/main_view.php</dfn></strong> and add the following code.

&lt;?php

$id='';

?&gt;

&lt;html&gt;

&lt;head&gt;

&lt;title&gt;My View&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to Main View&lt;/h1&gt;

&lt;table border="1"&gt;
&lt;tr&gt;
&lt;th&gt;UserName&lt;/th&gt;
&lt;th&gt;First Name&lt;/th&gt;
&lt;th&gt;Last Name&lt;/th&gt;
&lt;th&gt;Gender&lt;/th&gt;
&lt;th&gt;Mobile No&lt;/th&gt;
&lt;th colspan="2"&gt;Operation(s)&lt;/th&gt;
&lt;/tr&gt;
&lt;?php

foreach($res-&gt;result() as $row)

{ ?&gt;

&lt;tr&gt;
&lt;td&gt;&lt;?php echo $row-&gt;username; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $row-&gt;first_name; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $row-&gt;last_name; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $row-&gt;gender; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?php echo $row-&gt;mno; ?&gt;&lt;/td&gt;

&lt;td&gt;&lt;a href=" &lt;?php echo base_url() ?&gt;/index.php/main/operation?id=&lt;?php echo                    $row-&gt;id; ?&gt;"&gt;Edit&lt;/a&gt;&lt;/td&gt;
&lt;?php $id1=$row-&gt;id;?&gt;
&lt;td&gt;&lt;a href="&lt;?php echo base_url() ?&gt;/index.php/main/delete/&lt;?php echo $id1 ?&gt;"

class="delete"&gt;Delete&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
}
?&gt;

&lt;tr align="center"&gt;

&lt;td colspan="7"&gt;&lt;a href="&lt;?php echo base_url() ?&gt;/index.php/main/operation"&gt;Add              New&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;

&lt;/body&gt;

&lt;/html&gt;

<strong>5)</strong>   Open your browser to <strong>http://localhost/CodeIgniter/index.php/main/display </strong> You should get like this as seen below
<h3><strong>Output Result:-</strong></h3>
<ul>
	<li>
<h5>Display All Record:</h5>
</li>
</ul>
&nbsp;

<a href="http://www.wamasoftware.com/blog/wp-content/uploads/2014/04/My-View.png"><img class="alignnone  wp-image-329" alt="My View" src="http://www.wamasoftware.com/blog/wp-content/uploads/2014/04/My-View-300x130.png" width="325" height="150" /></a>
<ul>
	<li>
<h5>Insert  Record:</h5>
</li>
</ul>
<a href="http://www.wamasoftware.com/blog/wp-content/uploads/2014/04/localhost-my-index.php-main-operation1.png"><img class="alignnone  wp-image-327" alt="localhost my  index.php main operation" src="http://www.wamasoftware.com/blog/wp-content/uploads/2014/04/localhost-my-index.php-main-operation1-300x130.png" width="325" height="150" /></a>
                                    


Categories

CodeIgnitor

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote