简体   繁体   中英

codeigniter how do I edit or delete the rows in table

How do I get the user id of the respective rows of table to edit or delete? My table has an action column that have edit and delete button. This is my view:

 <table class="table table-striped">
   <tr>
     <td>First Name</td>
     <td>Last Name</td>
     <td>Address Name</td>
     <td>Action</td>
   </tr>
  <?php foreach($results as $row): ?>
    <tr>
      <td><?php echo $row->first_name; ?></td>
      <td><?php echo $row->last_name; ?></td>
      <td><?php echo $row->address; ?></td>
      <td><a href="" class="btn btn-info">Edit</a>
      <a href="<?php echo base_url()."main/deleteclient" ?>"                                       
          class="btn btn-danger"
              onclick="return confirm
              ('Are you sure  to Delete?')">Delete</a></td>

                </tr>
                 <?php endforeach; ?>

 </table> 

Change

<a href="<?php echo base_url()."main/deleteclient" ?>"                                       
          class="btn btn-danger"
          onclick="return confirm
          ('Are you sure  to Delete?')">Delete</a>

to

<a href="<?php echo base_url()."main/deleteclient?id=".$row->id ?>"                                       
          class="btn btn-danger"
          onclick="return confirm
          ('Are you sure  to Delete?')">Delete</a>

And then from your PHP script check if the variable "id" exists in your $_GET and handle the deletion.

Still working on formatting the answer.

    As like you are getting other table values like first name and last name there should be (if you gave an primary id) an id for each row. So you can get that id for each row and send it to your delete function by URL.

     <?php foreach($results as $row): ?>
           <tr> 

                <td><?php echo $row->first_name; ?></td>
                <td><?php echo $row->last_name; ?></td>
                <td><?php echo $row->address; ?></td>
                <td><a href="" class="btn btn-info">Edit</a>

<a href="<?= base_url();?>main/deleteclient/<?= $row->id;?>"></a>                                      
                  class="btn btn-danger"
                  onclick="return confirm
                  ('Are you sure  to Delete?')">Delete</a></td>

                    </tr>
                     <?php endforeach; ?>

ADD $row->id IN YOUR TAGS and this will take the row id to your deleteclient function in your main controller when you click that button.

thank y'all so much.. This is how i deleted rows. Let me know if it there are better ways to do it!

   view: 

    <table class="table table-striped">
      <tr>
       <td>First Name</td>
        <td>Last Name</td>
        <td>Address</td>
         <td>Citizen Number</td>
       <td>Action</td>
   </tr>
    <?php foreach($results as $row): ?>
   <tr> 

   <td><?php echo $row->first_name; ?></td>
   <td><?php echo $row->last_name; ?></td>
   <td><?php echo $row->address; ?></td>
  <td><?php echo $row->citizen_no; ?></td>
  <td><a href="" class="btn btn-info">Edit</a> 
  <a href="<?php echo base_url().
  "main/deleteclient?id=".$row->client_id ?  >" 
  class="btn btn-danger" 
 onclick="return confirm('Are you sure to Delete?')">Delete</a></td>

 </tr>
<?php endforeach; ?>

 </table>  

    controller:
      public function deleteclient(){
     if(isset($_GET['id'])){
        $id=$_GET['id'];
        $this->load->model('members');
        $this->members->row_delete($id);
        redirect('main/viewclient');}
 }
   model:
  public function row_delete(){
        $id=$_GET['id'];
        $this->db->where('client_id', $id);
        $this->db->delete('client');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM