codeigniter如何编辑或删除表中的行


codeigniter how do I edit or delete the rows in table

如何获取要编辑或删除的表的相应行的用户id?我的表有一个具有编辑和删除按钮的操作列。这是我的观点:

 <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> 
    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; ?>

在标签中添加$row->id,当您单击该按钮时,这将把行id带到主控制器中的deleteclient函数中。

更改

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

<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>

然后从PHP脚本中检查$_GET中是否存在变量"id",并处理删除操作。

仍在格式化答案。

非常感谢大家。。这就是我删除行的方式。如果有更好的方法,请告诉我!

   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');
    }