【问题标题】:codeigniter how do I edit or delete the rows in tablecodeigniter如何编辑或删除表中的行
【发布时间】:2017-07-06 03:18:07
【问题描述】:

如何获取表格各行的用户 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> 

【问题讨论】:

  • 查询是否也返回ID?
  • 您不应使用链接(GET 请求)来删除内容。而是使用表单和 POST。
  • 你能发布你的控制器和模型吗?
  • 你的results中有id

标签: php codeigniter html-table edit crud


【解决方案1】:
    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 函数中。

【讨论】:

  • 查看此链接。您将了解 url 系统在 codeigniter 中的工作方式。如果您在阅读本文后需要进一步的帮助,请告诉我。 :) ellislab.com/codeigniter/user-guide/general/urls.html
  • 这篇文章很有帮助,但我仍然没有弄清楚如何在控制器中获取用户 ID。
  • 假设您有一个名为 main 的控制器,并且在该控制器中您有一个函数 deleteclient。所以会像这样- class Main extends CI_Controller{ public function deleteclient($id) { //你的 mysql 查询来删除 id。 } }。当您的用户单击 时,它将触发主控制器,删除客户端功能$row->id 作为 deleteclient($id) 函数的参数 $id。
【解决方案2】:

改变

<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 脚本中检查变量“id”是否存在于您的 $_GET 中并处理删除。

仍在努力格式化答案。

【讨论】:

  • 你选择了id字段(主键)吗??在模型中??如果您选择了该字段,那么它将显示在这里...
  • 这是我的模型:public function getall(){ $query= $this->db->query("select * from client");返回$查询->结果(); }
  • 好的,查询看起来不错..你得到结果了吗??我的意思是名字,姓氏等?
  • 你想通过ajax删除它??或者您想将其重定向到页面并捕获 Id 然后删除并返回此列表页面??
  • 好的,然后它简单地创建一个函数 deleteclient 并取最后一个作为段的值。因为你在最后传递了值,它会类似于 deleteclient/2 或 deleteclient /4 .....这些只是行的ID。得到这个并触发删除查询并放置重定向代码以再次返回此页面......
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 2019-02-22
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多