【问题标题】:Delete using checkbox in codeigniter使用 codeigniter 中的复选框删除
【发布时间】:2015-07-27 03:33:22
【问题描述】:

我在 codeigniter 中删除复选框时遇到问题。我有一个表格,其中显示了一个表格并填充了数据库中的数据,并且有两个提交按钮。第一个是我已经做过的 adduser 按钮。

我的问题是我不知道如何在显示的表格中获取复选框的值。这是 sn-p 以了解更多我的问题:

控制器:

public function options()
    {
        $data['meta_title']="Users";
        $data['meta_desc']="Find your friends from our members";

        if ($this->input->post('AddUser'))
        {
            $this->load->view('template/header',$data);
            $this->load->view('users/add_view');
            $this->load->view('template/footer');
        }
        elseif($this->input->post('Delete'))
        {
            /// i have troubles with my logic here
        }
    }

查看:

<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo anchor('users/edit/'.$row['usrID'],$row['usrName']);?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>
            <td><input type="checkbox" name="checkID[]" id="checkID" value="<?php echo $row['usrID'];?>" />

              <label for="checkID"></label></td>
        </tr>
<? endforeach; ?>

【问题讨论】:

    标签: php database codeigniter checkbox


    【解决方案1】:

    如果您实际上也添加了提交按钮,会更容易理解您的问题。

    我将尝试向您解释这些原则; 您需要有一个表格来包装您要发送的数据,例如:

    <form action="whereToSendTheData.php" method="post">
        <table>
            <tr class="even gradeC">
                <td><?php echo anchor('users/edit/'.$row['usrID'],$row['usrName']);?></td>
                <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
                <td><?php echo $row['usrpBday'];?></td>
                <td><?php echo $row['usrpSex'];?></td>
                <td><?php echo $row['usrpAddress'];?></td>
                <td><input type="checkbox" name="checkID" id="checkID" value="<?php echo $row['usrID'];?>" />
    
                <label for="checkID"></label></td>
            </tr>
        </table>
        <input type="submit" value="Add user">
    </form>
    

    此脚本会将表单中的数据发送到页面“whereToSendTheData.php”,我在上面的“action”属性中定义。

    关于“whereToSendData.php” 您需要有一个脚本来处理/获取此数据。 例如:

    <?php
    echo "hello $_POST['checkID']";
    ?>
    

    这将输出“hello”和checkID的值。

    您可以在此处阅读有关该主题的更多信息: http://www.w3schools.com/php/php_post.asp

    【讨论】:

    • 是的,我有表格..这就是为什么我把它称为代码 sn-p 因为它不是整个代码
    • 好的,但是它回答了你的问题吗?还是你还有问题?
    【解决方案2】:

    你最好使用

    <form method="post/get" action="url to ur controller">
       //some code to move data to controller
       <submit button>
    </form>
    

    并在你的控制器中放置一个删除函数:

    public function delete()
    {
       //get the id's of rows which are checked and delete them as
       $this->db->where('userid in','array(checked ids)');
    }
    

    【讨论】:

      【解决方案3】:

      您做得很好,将输入名称设置为数组是正确的选择。

      现在,要获取所选复选框的值,您可以这样做:

      $selectedUsers = $this->input->get('checkID') //如果你设置了form的方法为GET

      $selectedUsers = $this->input->post('checkID') //如果你设置了form的方法为POST

      如果你有这个功能,那么在你的模型中

      /**
       * @param array $values
       * @param string $field
       *
       * @return bool
       */
      public function remove_in($values, $field)
      {
          if ($values === false) {
              return FALSE;
          }
      
          $this->db->where_in($field, $values);
      
          return $this->db->delete($this->table);
      }
      

      您可以从控制器中调用它,例如:

      $this->user_model->remove_in($selectedUsers, 'user_id');

      假设您的模型名称是 user_model 您已使用 $this-&gt;load-&gt;model('user_model') 加载,并且您的用户 ID 字段的名称是 user_id

      享受吧!


      注意:虽然这与你的情况无关,但你不应该为多个 HTML 元素使用相同的 id。相反,如果不需要,您可以省略 id 部分:

      <input type="checkbox" name="checkID[]" value="<?php echo $row['usrID'];?>" />
      

      或者如果你需要一个 ID,你应该使用如下:

      <input type="checkbox" name="checkID[]" id="checkID_<?php echo $row['usrID'];?>" value="<?php echo $row['usrID'];?>" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多