溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在thinkPHP項目中實現(xiàn)一個批量刪除功能

發(fā)布時間:2021-01-28 11:06:29 來源:億速云 閱讀:159 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹怎么在thinkPHP項目中實現(xiàn)一個批量刪除功能,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

html:

<li>
  <a class="delete" href="__URL__/deleteSelected/navTabId/__MODULE__" target="selectedTodo" posttype="string" calback="navTabAjaxMenu" rel='ids' title="你確定要刪除嗎?" warn="請選擇節(jié)點"><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:

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('非法操作');
      }
    }
}

原理是根據(jù)Web表單提交時可以傳遞數(shù)組,例如:

<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(
  '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'
    )
  )
)

關(guān)于怎么在thinkPHP項目中實現(xiàn)一個批量刪除功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI