随着科技的飞快发展,技术的不断更新迭代,TP5"敲敲"来到我们的生活,让我带你ECHO出它的神奇吧!
一 添加/修改/文件上传
public function addPlus(){
$post=input('post.'); //接值
$file = request()->file('img'); // 获取表单上传文件img的值
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->validate(['size'=>15678,'ext'=>'jpg,png,gif'])
->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
// 成功上传后 获取上传信息
$post['u_img']= $info->getFilename();
if(empty($post['u_id'])){
//添加入库
$result=db('user')->insert($post);
}else{
//修改资料
$result=db('user')->update($post);
}
if($result){
$this->success('操作成功','listPage');
}else{
$this->error('失败');
}
}else{
// 上传失败获取错误信息
echo $file->getError();
}
}
二 查询
function user(){
$user=db('user');
$result= $user->where('u_pwd',$pwd)
->whereOr('u_name|u_phone',$name)
->find();
echo $user->getLastSql();die;
}
//SELECT * FROM `user` WHERE ( `u_name` ='YBaog12' OR `u_phone` = 1379999999 ) AND `u_pwd`='123456';
三 分页
public function listPage(){
$list = db('user')
->where('status',1) //查询条件
->order('sort desc') //排序
->paginate(10); //每页显示条数
return $this->fetch('list',['list'=>$list]);
分页前台样式
<style type="text/css">
.pagination{text-align:center;margin-top:20px;margin-bottom: 20px;}
.pagination li{margin:0px 10px; border:1px solid #e6e6e6;padding: 3px 8px;display: inline-block;}
.pagination .active{background-color:#1d53bf;color: #fff;}
.pagination .disabled{color:#aaa;}
</style>
<div>{$list->render()}</div>
四 删除
public function listDel(){
$id=input('get.sid'); //获取删除ID
$del=db('user')->delete($id); //根据主键删除(可以使用 Where)
if($del){
unlink('文件的根路径');
$this->success('删除成功','listPage');
}else{
$this->error('失败');
}
}
JS删除一行
function deleteCurrentRow(obj){
var tr=obj.parentNode.parentNode;
var tbody=tr.parentNode;
tbody.removeChild(tr);
//只剩行首时删除表格
// if(tbody.rows.length==1) {
// tbody.parentNode.removeChild(tbody);
// }
}
<td><button οnclick="deleteCurrentRow(this)">删除</button></td>