abstract:1.控制類代碼<?phpnamespace app\admin\controller;use app\admin\controller\Common;use app\admin\model\SlideModel;use think\facade\Request;use think\facade\Session;class Slide extends Common{
1.控制類代碼
<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\SlideModel;
use think\facade\Request;
use think\facade\Session;
class Slide extends Common
{
public function index()
{
//實例化模型
$slide = new SlideModel();
// 查詢數(shù)據(jù)按照id排序并且每頁顯示四條數(shù)據(jù)
$slides = $slide->order('id', 'desc')->paginate(4);
//將數(shù)據(jù)復(fù)制到模板
$this->view->slides = $slides;
// 渲染模板
return $this->fetch();
}
public function add()
{
// 渲染模板
return $this->fetch();
}
public function DoAdd()
{
// 獲取提交過來的數(shù)據(jù)
$data = Request::param();
// 加入添加時間
$data['time'] = time();
// 加入添加管理員
$data['username'] = Session::get('username');
// 實例化模型
$slide = new SlideModel();
// 存儲并驗證
if ($slide->save($data)) {
// 返回對應(yīng)信息
return ['res' => 1, 'msg' => '添加成功'];
} else {
return ['res' => 0, 'msg' => '添加失敗'];
}
}
public function upload()
{
//獲取上傳圖片的信息
$file = Request::file('file');
// 驗證圖片并移動到指定目錄
if ($info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move('upload')) {
$fileName = '/upload/' . $info->getSaveName();
return json([1, '上傳成功', 'data' => $fileName]);
} else {
// 返回上傳失敗錯誤信息
return $file->getError();
}
}
public function del()
{
// 獲取需要刪除的id
$slideId = Request::param('id');
//實例化模型
$slide = new SlideModel();
// 刪除并驗證
if ($slide->destroy($slideId)) {
return ['res'=>1,'msg'=>'刪除成功!'];
}
}
}
2.模型類代碼
<?php
namespace app\admin\model;
use \think\Model;
class SlideModel extends Model
{
protected $table = 'slide';
protected $pk = 'id';
}
3.視圖類代碼
<!doctype html>
<html lang="en">
{include file="/public/header"}
<body>
<div class="x-nav">
<span class="layui-breadcrumb">
<a href="">首頁</a>
<a href="">演示</a>
<a>
<cite>導(dǎo)航元素</cite></a>
</span>
<a class="-layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
href="javascript:location.replace(location.href);" title="刷新">
<i class="layui-icon" style="line-height:30px">?</i></a>
</div>
<div class="x-body">
<xblock>
<button class="layui-btn" onclick="x_admin_show('添加輪播圖','{:url(\'add\')}')"><i class="layui-icon"></i>添加
</button>
<span class="x-right" style="line-height:40px">共有數(shù)據(jù):88 條</span>
</xblock>
<table class="layui-table layui-form">
<thead>
<tr>
<th width="70">輪播圖ID</th>
<th>輪播圖</th>
<th width="200">輪播圖說明</th>
<th width="200">管理員</th>
<th width="200">發(fā)布時間</th>
<th width="200">操作</th>
</thead>
<tbody>
{volist name="slides" id="slide"}
<tr>
<td>{$slide.id}</td>
<td>
<img src="{$slide.pic}">
</td>
<td>{$slide.desc}</td>
<td>{$slide.username}</td>
<td>{$slide.time|date="Y-m-d"}</td>
<td class="td-manage">
<button class="layui-btn-danger layui-btn layui-btn-xs" onclick="member_del(this,'{$slide.id}')"
href="javascript:;"><i class="layui-icon"></i>刪除
</button>
</td>
</tr>
{/volist}
</tbody>
</table>
<div class="page">
<div>
</div>
</div>
</div>
<script>
layui.use('laydate', function () {
var laydate = layui.laydate;
//執(zhí)行一個laydate實例
laydate.render({
elem: '#start' //指定元素
});
//執(zhí)行一個laydate實例
laydate.render({
elem: '#end' //指定元素
});
});
/*用戶-刪除*/
function member_del(obj, id) {
layer.confirm('確認(rèn)要刪除嗎?', function (index) {
//發(fā)異步刪除數(shù)據(jù)
$.get('{:url(\'del\')}', 'id=' + id, function (data) {
if (data.res == 1) {
$(obj).parents("tr").remove();
layer.msg('已刪除!', {icon: 1, time: 1000});
} else {
$(obj).parents("tr").remove();
layer.msg('刪除失敗!', {icon: 1, time: 1000});
}
});
});
}
</script>
</body>
</html>
4.運行結(jié)果
Correcting teacher:天蓬老師Correction time:2019-05-28 16:57:22
Teacher's summary:通過這個小案例, 相信你對web開發(fā)有了一個深刻 的認(rèn)識, 借助一些框架, 可以極大的提升開發(fā)效率