繁体   English   中英

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> 

更改

<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”并处理删除。

仍在格式化答案。

    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函数中。

非常感谢你们..这就是我删除行的方式。 让我知道是否有更好的方法!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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