Table of Contents
1. Simple text class addition (name, age, title, introduction, etc.)
2. Edit and add long text (article, preface, introduction, etc.)
3.0-1 Select to add (gender, yes/no addition)
4. Date addition (manual addition and automatic addition)
5. Add pictures
6. Add mobile phone number and email address
Home Backend Development PHP Tutorial Rules for adding different data in thinkphp5

Rules for adding different data in thinkphp5

Jun 15, 2018 am 11:16 AM
thinkphp

When the admin system administrator adds data tables, since different data will have different adding methods, different data also have different database storage types, the following are several common different types of data adding rules //Not yet The data types stored in the noted database are all char

1. Simple text class addition (name, age, title, introduction, etc.)

add.html code segment

<p class="col-sm-6">
                                <input class="form-control" id="username" placeholder="" name="username"  type="text">
                            </p>
Copy after login

Admin.php function part

 public function add()
    {   
        if(request()->isPost()){            $data=[//将输入的值赋值给数组
                &#39;username&#39;=>input(&#39;username&#39;),                &#39;password&#39;=>input(&#39;password&#39;),
            ];            $validate = \think\Loader::validate(&#39;Admin&#39;);//验证环节
            if(!$validate->scene(&#39;add&#39;)->check($data)){               $this->error($validate->getError()); die;//未通过验证则输出错误
            }            if(db(&#39;admin&#39;)->insert($data)){//添加数据库
                return $this->success(&#39;添加管理员成功!&#39;,&#39;lst&#39;);
            }else{                return $this->error(&#39;添加管理员失败!&#39;);
            }            return;
        }        return $this->fetch();
    }
Copy after login

Admin.php
File

<?phpnamespace app\admin\validate;use think\Validate;class Admin extends Validate{
    protected $rule = [//验证条件
        &#39;username&#39;  =>  &#39;require|max:25|unique:admin&#39;,        &#39;password&#39; =>  &#39;require&#39;,
    ];    protected $message  =   [//报错信息
        &#39;username.require&#39; => &#39;管理员名称必须填写&#39;,        &#39;username.max&#39; => &#39;管理员名称长度不得大于25位&#39;,        &#39;username.unique&#39; => &#39;管理员名称不得重复&#39;,        &#39;password.require&#39; => &#39;管理员密码必须填写&#39;,
    ];    protected $scene = [        &#39;add&#39;  =>  [&#39;username&#39;=>&#39;require|unique:admin&#39;,&#39;password&#39;],        &#39;edit&#39;  =>  [&#39;username&#39;=>&#39;require|unique:admin&#39;],
    ];//约束条件所作用的函数域}
Copy after login

Add renderings

Rules for adding different data in thinkphp5

Rules for adding different data in thinkphp5

2. Edit and add long text (article, preface, introduction, etc.)


Long text usually refers to articles, introductions and other texts that need to be formatted. Here you can refer to some plug-ins. The project refers to Baidu Editor. Place the downloaded Baidu Editor file in public/static/admin.
As shown below
Rules for adding different data in thinkphp5


The following is the method to reference the editor

Rules for adding different data in thinkphp5

add.html The code style

 <p class="col-sm-6">
                                 <label >
                                    <textarea name="content"  id="content" ></textarea>
                                 </label>
                            </p>
Copy after login

The function in the controller is read in the same way as the short text, because they are all of char type
Rules for adding different data in thinkphp5

Complete rendering
Rules for adding different data in thinkphp5

3.0-1 Select to add (gender, yes/no addition)


What is used here is the plug-in in beyond.js
As shown in the picture
Rules for adding different data in thinkphp5


The source code is

 <p class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">状态</label>
                            <p class="col-sm-6">
                                <select name="gender">
                                   <option value="请选择状态">请选择状态</option>

                                    <option value="已审核">已审核</option>
                                    <option value="未审核">未审核</option>


                                </select>
                            </p>
                        </p>

                         <p class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">性别</label>
                                <p class="col-sm-6">
                                 <p class="control-group">
                                                <p class="radio">
                                                    <label>
                                                        <input name="form-field-radio" type="radio" class="colored-blue" value="男">
                                                        <span class="text" >男</span>
                                                    </label>
                                                </p>

                                                <p class="radio">
                                                    <label>
                                                        <input name="form-field-radio" type="radio" class="colored-danger"  value="女">
                                                        <span class="text"> 女</span>
                                                    </label>
                                                </p>

                                                <p class="radio">
                                                    <label>
                                                        <input name="form-field-radio" type="radio" class="colored-success"  value="未确定">
                                                        <span class="text"> 未确定</span>
                                                    </label>
                                                </p>


                                            </p>
                                 </p>
                        </p>
Copy after login

It is best to use the char type for this gender recommendation class. It is a bit troublesome to use integer characters. , this statically provides option categories, and there is also a category of categories that is read in from the database
As follows
Rules for adding different data in thinkphp5

The code in the html is as follows

  <p class="form-group">
                            <label for="group_id" class="col-sm-2 control-label no-padding-right">所属栏目</label>
                            <p class="col-sm-6">
                                <select name="cateid">
                                    <option value="">请选择栏目</option>
                                    {volist name="cateres" id="vo"}                                    <option value="{$vo.id}">{$vo.catename}</option>
                                    {/volist}                                </select>
                            </p>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </p>
Copy after login

cates source Article.php
As shown below
Rules for adding different data in thinkphp5
Because in the Article controller, the artcle data table has been connected to the model by default, but it is recommended to use the belongsTo() function in model/Article.php from other data tables. Establish a one-to-many connection as shown in the figure (one page connects multiple data tables, my understanding seems not correct) as shown below
Rules for adding different data in thinkphp5
Such a multi-select type is ready

4. Date addition (manual addition and automatic addition)


When adding dates, the date type corresponding to the general database is
Rules for adding different data in thinkphp5


The simplest way is to add it automatically
There is no need for an input box. This kind of addition is usually a fixed time for the system, or to obtain the current time. In the controller, you only need to use functions or customized time, for example, use date("Y-m-d H:i:s"); to obtain the current time. Time

To add time manually, enter the current time in the input box yourself
Requires a date template plug-in, such as the date plug-in in layui
Rules for adding different data in thinkphp5

Rules for adding different data in thinkphp5

Since it is necessary to import css js, etc., I will not explain it here. When it is useful in the future, I will explain it specifically

5. Add pictures


The first is the type of picture in the database
Rules for adding different data in thinkphp5


The code in add.html

 <p class="form-group">
                            <label for="group_id" class="col-sm-2 control-label no-padding-right">缩略图</label>
                            <p class="col-sm-6">
                                <input id="pic" placeholder="" name="pic"  type="file">
                            </p>
                        </p>
Copy after login

The following picture shows the controller code, which includes More detailed comments (own understanding)
Rules for adding different data in thinkphp5


6. Add mobile phone number and email address


This is the same as adding short text, mainly to determine whether the entered information is a mobile phone number or email address
In validate/Article.php
Add verification information similar to that in the manual for verification
Rules for adding different data in thinkphp5

This article explains the rules for adding different data in thinkphp5. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Related operations about the ThinkPHP5 database

About the database and model usage of ThinkPHP5

A case study on thinkphp5.0 database operation

The above is the detailed content of Rules for adding different data in thinkphp5. 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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
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.

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.

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.

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.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles