Blog Details

ADD-Update-Delete Operation In Codeigniter



                                

* Creating First Application at CodeIgniter:-

1)  First, we make controller, create a file name "pages.php"   Create a file    at application/controllers/main.php with the following code. <?php class main extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->model('main_model'); } public function display() { $data['res']=$this->main_model->get_data(); $this->load->view('main_view',$data); } public function operation() { if(isset($_POST['btn'])) { if(empty($_POST['id'])) { $this->main_model->add_data($_POST); } else { $this->main_model->edit_data($_POST); } $this->display(); } else { $id=$this->input->get('id'); if($id) { $data['value']=$this->main_model->get_data_id($this->input->get('id')); $this->load->view('form1',$data); } else { $this->load->helper('form'); $this->load->library('form_validation'); $this->load->view('form1'); }}} public function delete($id) { $delete=$this->main_model->delete_data($id); $this->display() } public function index() { $this->load->view('form1'); } } ?> 2)  First, we make model, create a file name "main_model.php"   Create a file    at application/model/main_model.php with the following code. <?php class main_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->database(); } function get_data() { $query = $this->db->get('register'); return $query; } function get_data_id($id) { $query = $this->db->get_where('register',array('id' => $id),1); return $query; } function delete_data($id) { $this->db->delete('register',array('id'=>$id)); } function add_data($data) { $value=array('username'=>$data['txt_user'],'first_name'=>$data['txt_fname'], 'last_name'=>$data['txt_lname'],'gender'=>$data['gender'],'mno'=>$data['txt_mno']); $this->db->insert('register',$value); } function edit_data($data) { $value=array('username'=>$data['txt_user'],'first_name'=>$data['txt_fname'], 'last_name'=>$data['txt_lname'],'gender'=>$data['gender'],'mno'=>$data['txt_mno']); $this->db->update('register',$value,array('id' => $data['id'])); }} ?> 3) Create the form at application/views/form1.php and add the following code. <?php $username=''; $fname=''; $lname=''; $mno=''; $id=''; $submit='Add User'; if(isset($value) && !empty($value)) { foreach($value->result() as $row) { $username=$row->username; $fname=$row->first_name; $lname=$row->last_name; $mno=$row->mno; $id=$row->id; $submit='Edit User'; i f($row->gender=='Male') {?> <script> document.getElementById('gender_male').checked=true</script> <?php } else { ?> <script> document.getElementById('gender_female').checked=true </script> <?php } } } ?> <html> <head> <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script> </head> <body> <form name="ajaxform" id="ajaxform" action="<?php echo base_url() ?>/index.php/main/operation" method="POST"> <table border="1"> <tr> <th>UserName</th> <td><input type="text" name="txt_user" value="<?php echo $username; ?> "                              /></td> </tr> <tr> <th>First Name</th> <td><input type="text" name="txt_fname" value="<?php echo $fname; ?> "                                  /></td> </tr> <tr> <th>Last Name</th> <td><input type="text" name="txt_lname" value="<?php echo $lname; ?> "                                  class="required"/></td> </tr> <tr> <th>Gender</th> <td> <input type="radio" id="gender_male" name="gender" value="Male"/>Male <input type="radio" id="gender_female" name="gender" value="Female"/>Female </td> </tr> <tr> <th>Mobile No</th> <td><input type="text" name="txt_mno" value="<?php echo $mno; ?> "                                          /></td> </tr> </table> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="hidden" id="btn" value="<?php echo $submit; ?>" name="btn"/> <input type="submit" id="simple-post" value="<?php echo $submit; ?>"                                         name="simple-post"/> </form> </body> </html> 4) Create the form at application/views/main_view.php and add the following code. <?php $id=''; ?> <html> <head> <title>My View</title> </head> <body> <h1>Welcome to Main View</h1> <table border="1"> <tr> <th>UserName</th> <th>First Name</th> <th>Last Name</th> <th>Gender</th> <th>Mobile No</th> <th colspan="2">Operation(s)</th> </tr> <?php foreach($res->result() as $row) { ?> <tr> <td><?php echo $row->username; ?></td> <td><?php echo $row->first_name; ?></td> <td><?php echo $row->last_name; ?></td> <td><?php echo $row->gender; ?></td> <td><?php echo $row->mno; ?></td> <td><a href=" <?php echo base_url() ?>/index.php/main/operation?id=<?php echo                    $row->id; ?>">Edit</a></td> <?php $id1=$row->id;?> <td><a href="<?php echo base_url() ?>/index.php/main/delete/<?php echo $id1 ?>" class="delete">Delete</a></td> </tr> <?php } ?> <tr align="center"> <td colspan="7"><a href="<?php echo base_url() ?>/index.php/main/operation">Add              New</a></td> </tr> </table> </body> </html> 5)   Open your browser to http://localhost/CodeIgniter/index.php/main/display  You should get like this as seen below

Output Result:-

  • Display All Record:
  My View
  • Insert  Record:
localhost my  index.php main operation


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