thinkphp5的入门学习(1)引入数据列表及其操作
来源:互联网 发布:游戏寻路算法 编辑:程序博客网 时间:2024/05/18 20:46
此目的为如下路的数据表,并对表中数据增删改,表中数据为数据库读入的数据,
0.连接数据库
1. 数据读入
在文章控制器Article中创建文章主页函数 lst()
/ /use app\Admin\model\Article as ArticleModel;
$list = ArticleModel::paginate(3);//数据读入,继承model,每页显示数据 $this->assign('list',$list); return $this->fetch();
在view/article/lst.html中的写法
1.数据添加
首先在view/article/lst.html的添加按钮代码为
<button type="button" tooltip="添加文章" class="btn btn-sm btn-azure btn-addon" onClick="javascript:window.location.href = '{:url('article/add')}'"> <i class="fa fa-plus"></i> Add</button>
调用article控制器之中的add函数
add函数如下图
add.html代码
add.html效果图
下面对add()函数语句的详细注释
public function add() { if(request()->isPost())//如果为post提交则 $data=[//输入的·数据赋值于数组中input('html元素name') 'title'=>input('title'), 'author'=>input('author'), 'desc'=>input('desc'), 'keywords'=>str_replace(',', ',', input('keywords')), 'content'=>input('content'), 'cateid'=>input('cateid'), 'time'=>time(),//时间的不需要输入的值进行函数赋值 ]; if(input('state')=='on'){//0-1判断器 $data['state']=1; } if($_FILES['pic']['tmp_name']){//插入图片 $file = request()->file('pic'); $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads'); $data['pic']='/uploads/'.$info->getSaveName(); } $validate = \think\Loader::validate('Article');//验证器 if(!$validate->scene('add')->check($data)){ $this->error($validate->getError()); die; } if(db('Article')->insert($data)){//添加数据库 return $this->success('添加文章成功!','lst'); }else{ return $this->error('添加文章失败!'); } return; } $cateres=db('cate')->select();//查询更新 $this->assign('cateres',$cateres); return $this->fetch(); }
2.数据删除
和添加一样
首先获取要删除的id
在article.php中的删除函数
public function del(){ $id=input('id');//获取需要删除的id if(db('Article')->delete(input('id'))){ $this->success('删除文章成功!','lst'); }else{ $this->error('删除文章失败!'); } }
3.数据修改
修改的id
修改的html代码和界面
html界面
修改函数
public function edit(){ $id=input('id');//获取需要修改的id $articles=db('Article')->find($id);//打开数据库该位置 if(request()->isPost()){/如果为post请求则.. $data=[ 'id'=>input('id'), 'title'=>input('title'), 'author'=>input('author'), 'desc'=>input('desc'), 'keywords'=>str_replace(',', ',', input('keywords')), 'content'=>input('content'), 'cateid'=>input('cateid'), ]; if(input('state')=='on'){//0-1选择 $data['state']=1; }else{ $data['state']=0; } if($_FILES['pic']['tmp_name']){//图片输入 $file = request()->file('pic'); $info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads'); $data['pic']='/uploads/'.$info->getSaveName(); } $validate = \think\Loader::validate('Article'); if(!$validate->scene('edit')->check($data)){ $this->error($validate->getError()); die; } if(db('Article')->update($data)){ $this->success('修改文章成功!','lst'); }else{ $this->error('修改文章失败!'); } return; } $this->assign('articles',$articles); $cateres=db('cate')->select();//修改后重新获取数据库,并刷新 $this->assign('cateres',$cateres); return $this->fetch(); }
4.不同数据添加的规则
见下一篇
阅读全文
0 0
- thinkphp5的入门学习(1)引入数据列表及其操作
- thinkphp5的入门学习
- thinkphp5的入门学习(2)不同数据添加的规则
- ThinkPHP5常用数据操作
- thinkphp5学习入门
- thinkphp5.0学习(七):数据库操作
- 线性表的引入及其基本操作
- Vue.js Demo 数据及其列表操作
- 入门ThinkPHP5.0模型篇-对数据库的操作
- thinkphp5的数据库操作
- thinkphp5 入门 -1 基础
- Thinkphp5 引入第三方类库的方法
- thinkphp5的入门学习(3)系统登录的相关设置
- (ASP.NET MVC4 入门学习笔记)学习自己建项目2----引入css和JS的配置、DBHelper及其他
- Hibernate学习笔记(1)-----------jar包引入及入门
- ThinkPHP5的验证码操作
- THINKPHP5 入门 2-1 URL
- thinkphp5 之数据操作和模板输出
- Java工程师路线规划
- sql语句汇总
- Qt5美化界面1——标签设置图片(含GIF)
- 实数系与实数定理
- 为大家分享daloradius+freeradius+openvpn的安装脚本
- thinkphp5的入门学习(1)引入数据列表及其操作
- 商品秒杀流程图和功能图
- nohup command>/dev/null 2>&1 详解
- Centos7安装vim8.0 + YouCompleteMe
- linux脚本中判断命令是否存在 hash <the_command>
- java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
- block,inline和inline-block概念和区别
- 2、国庆快过了,没心没肺也难免
- 怎么利用经纬度超链接到百度地图中啊