服务器之家

服务器之家 > 正文

thinkPHP批量删除的实现方法分析

时间:2021-03-22 17:08     来源/作者:ifeixiang

本文实例讲述了thinkPHP批量删除的实现方法。分享给大家供大家参考,具体如下:

html:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<li>
  <a class="delete" href="__URL__/deleteSelected/navTabId/__MODULE__" target="selectedTodo" posttype="string" calback="navTabAjaxMenu" rel='ids' title="你确定要删除吗?" warn="请选择节点"><span>批量删除</span></a>
</li>
<table class="table" width="100%" layoutH="138">
    <thead>
      <tr>
        <th width="10"><input type="checkbox" class="checkboxCtrl" group="ids" /></th>
        <th width="60">编号</th>
      </tr>
    </thead>
    <tbody>
    <volist id="vo" name="list">
      <tr>
        <td><input name="ids" type="checkbox" value="{$vo.id}"> </td>
        <td>{$vo['id']}</td>
      </tr>
    </volist>
</table>

php:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public function deleteSelected() {
    //删除指定记录
    $name = $this->getActionName();
    $model = D($name);
    if (!empty($model)) {
      $pk = $model->getPk();
      $ids = $_REQUEST['ids'];
      if (!empty($ids)) {
        $condition = array($pk => array('in', explode(',', $ids)));
        if (false !== $model->where($condition)->delete()) {
          $sql = $model->_sql();
          $this->success("删除成功!");
        } else {
          $this->error('删除失败!');
        }
      } else {
        $this->error('非法操作');
      }
    }
}

原理是根据Web表单提交时可以传递数组,例如:

?
1
2
3
4
5
6
7
8
9
10
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">
<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">
<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">
<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">

则传递过来的是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$_POST[] = array(
  'firstname'=>'value',
  'lastname'=>'value',
  'email'=>'value',
  'address'=>'value',
  'tree' => array(
    'tree1'=>array(
      'fruit'=>'value',
      'height'=>'value'
    ),
    'tree2'=>array(
      'fruit'=>'value',
      'height'=>'value'
    ),
    'tree3'=>array(
      'fruit'=>'value',
      'height'=>'value'
    )
  )
)

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部