Thursday, January 27, 2011

insert,delet,update








controller registration.php

<?php
  class Registration extends Controller
  {
 
    function Registration()
      {
       parent::Controller();
       $this->load->database();
       $this->load->model('registration_model');
       $this->load->helper('url');
       }
      function index()
       {
        $data=array();
        $submit = $this->input->post('submit');
        if($submit == 'Insert')
          {
          
            $name=$this->input->post('name');
            $age =$this->input->post('age');
            $gender=$this->input->post('gender');
            $address=$this->input->post('address');
             $this->registration_model->insert_values($name,$age,$gender,$address);
         
          }
       
           $submit=$this->input->post('delete');
           if($submit=='Delete')
            {
             $i=$this->input->post('index');
             $id =$this->input->post('id'.$i);
             $this->registration_model->delete_values($id);
            }
             $row=$this->registration_model->getusers();
             $data['query']=$row;
            $this->load->view('registration',$data);
     
      
            }
     
      function update($id=NULL)
      {
    $data=array();
   
    $submit=$this->input->post('submit');
           if($submit=='Update')
           {
            
             $name=$this->input->post('name');
             $age=$this->input->post('age');
             $gender=$this->input->post('gender');
             $address=$this->input->post('address');
                    
             $this->registration_model->update_values($name,$age,$gender,$address,$id);
               redirect('registration');
              }
             //$row=$this->registration_model->getusers();
             $row=$this->registration_model->getusers_byid($id);
             $data['query']=$row;
           
             $this->load->view('update_reg',$data);
     }
   
}
?>



create model registration_model.php


<?php
class Registration_model extends Model
{
   function Registration_model()
     {
       parent::Model();
     }
      
    function insert_values($name,$age,$gender,$address)
    {
     $data=array(
                  'name'=>$name,
                  'age'=>$age,
                  'gender'=>$gender,
                  'address'=>$address );
     $query=$this->db->insert('user',$data);             

    }  
   
      function getusers()
        {
       $query = $this->db->get('user');
       return $query->result();
       }
       function getusers_byid($id)
        {
          $this->db->where('id',$id);
          $query = $this->db->get('user');
          return $query->row();
       }
      
     function update_values($name,$age,$gender,$address,$id)
     {
   
        $data=array('name'=>$name,
                    'age'=>$age,
                   'gender'=>$gender,
                   'address'=>$address);
            $this->db->where('id',$id);
            $this->db->update('user',$data);
           
   
           
     }
     function delete_values($id)
     {
        $this->db->where('id', $id);
        $this->db->delete('user');
   
     }

}


?>


create view registration.php

<html>
<title>
Registration</title>
<body>
<form action="<?php echo base_url()?>registration" method="post">
<table border="1">
<tr><td>Username:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Age:</td><td><input type="text" name="age" /></td></tr>
<tr><td>Gender:</td><td><input type="text" name="gender" /></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="Insert" /></td>
</tr>
</table>
</form>
<table border="1">
<tr>
    <td>NAME</td>
    <td>AGE</td>
    <td>GENDER</td>
    <td>ADDRESS</td>
   
</tr>
<?php
$i="";
foreach($query as $value){?>
<form action="<?php echo base_url()?>registration" method="post">

<tr>
<td><?php echo $value->name;?></td>
<td><?php echo $value->age;?></td>
<td><?php echo $value->gender;?></td>
<td><?php echo $value->address;?></td>
<td><input type="hidden" name="id<?php echo $i;?>" value="<?php echo $value->id;?>" ></td>
    <input type="hidden" name="index" value="<?php echo $i;?>">
    <input type="hidden" name="delete" value="Delete">
<td><a href="<?php echo base_url()?>registration/update/<?php echo $value->id;?>">Update</a></td>
<td><input type="submit" name="submit" value="Delete<?php echo $i;?>"></td>
  </tr>
  </form>
<?php
}?>

</table>

</body>
</html>


create form update.php


<html>
<head>
</head>
<title>Update</title>
<body>
<form action="<?php echo base_url() ?>registration/update/<?php echo $query->id; ?>" method="post">

<table border="2" align="center">

<tr>
<td>NAME</td><td><input type="text" name="name" value="<?php echo $query->name; ?>" /></td></tr>
<tr><td>AGE</td><td><input type="text" name="age" value="<?php echo $query->age; ?>" /></td></tr>
<tr><td>GENDER</td><td><input type="text" name="gender" value="<?php echo $query->gender; ?>"/></td></tr>
<tr><td>ADDRESS</td><td><input type="text" name="address" value="<?php echo $query->address; ?>"/></td></tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Update" /></td>

</table>
</form>
</body>
</html>


No comments:

Post a Comment