0

I am making a table from a database and want to add an Edit/delete button to each row update or delete a specific row from the database. I have successfully added working "delete" button but I have no idea how could I update data in table <td> in view and send it to controller.

Here is my code:

view file

<table class="table table-striped">
  <tr>
    <td>name</td>
    <td>age</td>
    <td>gender</td>
    <td>class</td>
    <td>roll no.</td>
  </tr>
  <?php foreach($record as $r): ?>
  <tr>
   <td><?php echo $r->name; ?></td>
   <td><?php echo $r->age; ?></td>
   <td><?php echo $r->gender; ?></td>
   <td><?php echo $r->class; ?></td>
   <td><?php echo $r->roll no; ?></td>
   <td><a href="" >Edit</a>
     <a href="<?php echo base_url()."student/deleteRow" ?id="$r->name">"                                       
           onclick="return confirm
           ('Are you sure  to Delete?')"><i class="icon-trash"></a></td>

  </tr>
  <?php endforeach; ?>
</table> 

Controller Function

public function deleteRow(){

    if(isset($_GET['id'])){
      $id=$this->input->get('id');

      $this->student_model->rowDelete($id);

      redirect($_SERVER['HTTP_REFERER']);  
    }
}

I don't know how can I now insert an input field to update table row without effecting previous view. Any suggestion would be helpful.

1
  • See this menual. Commented Aug 5, 2017 at 13:17

3 Answers 3

1

To Edit the Studen data you need to pass an id or uniue column name to the data base to get that student data.

First Set the student id in <a href=""> tag.

<td><a href="<?= base_url('student/edit_student') ?>/$r->id" >Edit</a>

Then When you click on the edit it will take you to the controller. You can get the third url parameter direct in as show in the controler code: You can also use get as shon

Your Controller should be:

public function edit_student($id){

 $student_data = $this->student_model->get_student_data($id);

 $this->load->view('your_view',['student_data'=>$student_data)]);

}

Here is you model which get the id form controllr and find the student data and passit to back to the controller: Your Model should be:

public function get_student_data($id){

 $this->db->select('*');
 $this->db->from('your_table_name');
 $this->db->where('id',$id);
 $query = $this->db->get();

 $student_data = $query->$row_array();

 if(isset($student_data) && !empty($student_data)){

    return student_data;

 } else {

    return FALSE;

 }

}

Form controller you pass the data to the view. On View Side:

<?php 
// Just to ensure the data. Comment it after testing
echo "<pre>";
print_r($student_data);
echo "</pre>";

?>


<form action="<?= base_url('student/update_student') ?>/<?= $student_data['id'] ?>">

<input name="your_column_name" value="<?= $student_data['your_column_name'] ?>">
// Try to do the same for all you column.

<input type="submit" value="updata">

</form>

Here is the controller for update the data

public function update_student($id){

 $student_data = $this->input->post();  
 $status = $this->student_model->update_student($id,$student_data);

 if($status == TRUE){       
    $this->load->view('your_view');
    // Your Success view        
 } else {
    // Your view if fail to update
    $this->load->view('your_view');     
 }
}

Here is the model for update the data

public function get_student_data($id,$student_data){

 $this->db->where('id',$id);
 $this->db->update('your_table_name',$student_data);

    if($this->db->affected_rows() == 1){    
        return TRUE;    
    } else {
        return FALSE;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Very similar to what you have done for delete. Something like this:

<td>
    <a href="" >Edit/Delete</a>
    <a href="<?php echo base_url('student/updateRow?id='.$r->name); ?><i class="icon-edit"></a><!-- This should be another method in Student controller -->
    <a href="<?php echo base_url('student/deleteRow?id='.$r->name); ?>" onclick="return confirm('Are you sure  to Delete?')"><i class="icon-trash"></a><!-- I changed order of edit and delete -->
</td>

I need to warn you for CSRF. If you don't implement better security here, anyone pointing to that link would be able to edit or delete data. Check Security class in documentation and how to set hidden value so that way you would ensure that only one who has already requested that page was able to edit/delete rows.

Comments

0
<td><a href="<?php echo base_url();?>controller_name/function_name/<?php echo $edit_id;?>" >Edit</a></td>


another way 




<td><a href="<?php echo base_url();?>controller_name/function_name?id=<?php echo $edit_id;?>" >Edit</a></td>

You can use any one according to your requirement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.