Home Backend Development PHP Tutorial ThinkPHP implements batch deletion

ThinkPHP implements batch deletion

Jun 08, 2018 pm 03:38 PM
thinkphp batch deletion

This article mainly introduces the code examples to implement batch deletion based on ThinkPHP. It has certain reference value. Interested friends can refer to it.

This article provides an example analysis of the code to implement batch deletion based on ThinkPHP. Examples, shared with everyone for your reference, are as follows:

Without further ado, here are the renderings:


##HTML Layout (based on bootstrap)

<p class="panel panel-default">
  <p class="panel-heading">
    留言列表
    <a class="btn btn-xs btn-default pull-right" href="javascript:window.history.back();">返回</a>
    <a class="btn btn-xs btn-info pull-right mr-5" id="discard" href="javascript:;">删除</a>
  </p>
  <table class="table">
    <thead>
      <tr>
        <th><input class="all" type="checkbox"/></th>
        <th>id</th>
        <th>名称</th>
        <th>邮箱</th>
        <th>内容</th>
        <th>日期时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
       <form>
         <volist name="list" id="vo" empty="$empty">
           <tr>
             <td><input name="delete[]" type="checkbox" value="{$vo.id}" /></td>
             <td>{$vo.id}</td>
             <td>{$vo.name}</td>
             <td>{$vo.email}</td>
             <td>{$vo.subject}</td>
             <td>{$vo.datetime|date="Y-m-d H:i", ###}</td>
           <td>
            <a class="delete" href="javascript:;" data-id="{$vo.id}">删除</a>
           </td>
           </tr>
         </volist>
       </form>
    </tbody>
  </table>
</p>
Copy after login

JS script processing (using ajax technology)
First determine whether There is no selected value. If there is no value, it will prompt; if there is, it will be passed to the server for processing

/* 批量删除 */
  // 全选
  $(&#39;.all&#39;).click(function() {
    if($(this).is(&#39;:checked&#39;)) {
      $(&#39;:checkbox&#39;).attr(&#39;checked&#39;, &#39;checked&#39;);
    } else {
      $(&#39;:checkbox&#39;).removeAttr(&#39;checked&#39;);
    }
  });

  // 删除操作
  $(&#39;#discard&#39;).click(function() {
    if($(&#39;:checked&#39;).size() > 0) {
      layer.confirm(&#39;确定要删除吗?&#39;, {
        btn: [&#39;确定&#39;,&#39;取消&#39;], //按钮
        shade: false //不显示遮罩
      }, function(){
        $.post("{:U(&#39;Single/discard&#39;)}", {data: $(&#39;form&#39;).serializeArray()}, function(res) {
          if(res.state == 1) {
            layer.msg(res.message, {icon: 1, time: 1000});
          } else {
            layer.msg(res.message, {icon: 2, time: 1000});
          }
          setTimeout(function() {
            location.reload();
          }, 1000);
        });
      }, function(){
        layer.msg(&#39;取消了删除!&#39;, {time: 1000});
      });
    } else {
      layer.alert(&#39;没有选择!&#39;);
    }
  });
Copy after login

PHP code:
Get the submitted data, then loop to get the value of each id, and then perform the delete operation.

public function discard() {
  $contact = M(&#39;contact&#39;);
  $deleteArr = I(&#39;post.data&#39;);
  for($i=0;$i<count($deleteArr);$i++) {
    $contact->delete($deleteArr[$i][&#39;value&#39;]);
  }
  $this->ajaxReturn(array(&#39;message&#39;=>&#39;删除成功!&#39;));
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About thinkPHP’s method of implementing batch deletion

Analysis on thinkphp framework’s implementation of deletion and batch deletion

About the functional methods of adding and displaying data in the thinkphp framework

##

The above is the detailed content of ThinkPHP implements batch deletion. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Weibo posts in batches? -How to cancel your Weibo account? How to delete Weibo posts in batches? -How to cancel your Weibo account? Mar 18, 2024 pm 03:00 PM

How to delete Weibo posts in batches? To delete Weibo posts in batches, you can follow the steps below: Log in to your Weibo account and visit your personal center in the browser. Click More under Admin Center or Apps and select My Apps. Search and find the batch deletion Weibo tool in the App Market. Using this tool, you can choose to delete some Weibo posts or delete them all at once. After completing the deletion operation, if you need to restore the deleted content within 24 hours, you can use the member privilege &quot;Regret Medicine&quot;. This function cannot be used after 24 hours. In addition, there are other methods to achieve batch deletion of Weibo, such as using specific browser plug-ins, but these methods may not be officially supported and may pose security risks. Therefore, it is recommended to give priority to the official

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

Various ways to implement batch deletion operations in MyBatis Various ways to implement batch deletion operations in MyBatis Feb 19, 2024 pm 07:31 PM

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Detailed explanation of how to use MyBatis batch delete statements Detailed explanation of how to use MyBatis batch delete statements Feb 20, 2024 am 08:31 AM

Detailed explanation of how to use MyBatis batch delete statements requires specific code examples. Introduction: MyBatis is an excellent persistence layer framework that provides rich SQL operation functions. In actual project development, we often encounter situations where data needs to be deleted in batches. This article will introduce in detail how to use MyBatis batch delete statements, and attach specific code examples. Usage scenario: When deleting a large amount of data in the database, it is inefficient to execute the delete statements one by one. At this point, you can use the batch deletion function of MyBatis

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

See all articles