简体   繁体   English

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

[英]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? 如何获取表格各行的用户ID以进行编辑或删除? 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. 然后从您的PHP脚本中检查$ _GET中是否存在变量“ id”并处理删除。

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. 在您的标签中添加$ row-> id,当您单击该按钮时,会将行ID带到主控制器中的deleteclient函数中。

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

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

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