>php教程 >php手册 >thinkPHP如何实现批量删除的效果

thinkPHP如何实现批量删除的效果

PHPz
PHPz원래의
2016-06-13 10:37:03831검색

这篇文章主要介绍了thinkPHP批量删除的实现方法,结合实例形式分析了thinkPHP实现批量删除数据的数据库及模板操作相关技巧,需要的朋友可以参考下

html部分:

<li>
  <a class="delete" href="__URL__/deleteSelected/navTabId/__MODULE__" target="selectedTodo" posttype="string" calback="navTabAjaxMenu" rel=&#39;ids&#39; 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[&#39;id&#39;]}</td>
      </tr>
    </volist>
</table>

php部分:

public function deleteSelected() {
    //删除指定记录
    $name = $this->getActionName();
    $model = D($name);
    if (!empty($model)) {
      $pk = $model->getPk();
      $ids = $_REQUEST[&#39;ids&#39;];
      if (!empty($ids)) {
        $condition = array($pk => array(&#39;in&#39;, explode(&#39;,&#39;, $ids)));
        if (false !== $model->where($condition)->delete()) {
          $sql = $model->_sql();
          $this->success("删除成功!");
        } else {
          $this->error(&#39;删除失败!&#39;);
        }
      } else {
        $this->error(&#39;非法操作&#39;);
      }
    }
}

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

<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]">

则传递过来的是:

$_POST[] = array(
  &#39;firstname&#39;=>&#39;value&#39;,
  &#39;lastname&#39;=>&#39;value&#39;,
  &#39;email&#39;=>&#39;value&#39;,
  &#39;address&#39;=>&#39;value&#39;,
  &#39;tree&#39; => array(
    &#39;tree1&#39;=>array(
      &#39;fruit&#39;=>&#39;value&#39;,
      &#39;height&#39;=>&#39;value&#39;
    ),
    &#39;tree2&#39;=>array(
      &#39;fruit&#39;=>&#39;value&#39;,
      &#39;height&#39;=>&#39;value&#39;
    ),
    &#39;tree3&#39;=>array(
      &#39;fruit&#39;=>&#39;value&#39;,
      &#39;height&#39;=>&#39;value&#39;
    )
  )
)

【相关教程推荐】

1. php编程从入门到精通全套视频教程 

2. php从入门到精通  

3. bootstrap教程 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.