php主类型 子类型的增删改,分类模块增删改查

摘要:<?php

namespace app\admin\model;

use \think\Model;

class SortModel extends Model

{

protected $table = 'sort';

&<?php

namespace app\admin\model;

use \think\Model;

class SortModel extends Model

{

protected $table = 'sort';

protected $pk = 'id';

}<?php

namespace app\admin\controller;

use app\admin\controller\Common;

use app\admin\model\SortModel;

use think\facade\Request;

use think\facade\Session;

class Sort extends Common

{

public function index()

{

// 实例化模型

$sort = new SortModel();

// 查询数据并按照id的顺序每页八条数据

$sorts = $sort->order('id', 'desc')->paginate(8);

// 蒋数据赋值给模板

$this->view->sorts = $sorts;

// 渲染分类列表

return $this->fetch();

}

public function DoAdd()

{

// 获取提交过来的数据

$data = Request::param();

// 获取添加数据

$data['time'] = time();

// 获取发布管理员

$data['username'] = Session::get('username');

// 实例化模型

$sort = new SortModel();

// 存储并验证

if ($sort->save($data)) {

// 返回对应信息

return ['res' => 1, 'msg' => '添加成功!'];

} else {

return ['res' => 0, 'msg' => '添加失败!'];

}

}

public function edit()

{

// 获取需要修改的分类id

$sortId = Request::param('id');

// 使用分类的id查询对应的数据

$sort = SortModel::get($sortId);

// 将数据赋值给模板

$this->view->sort = $sort;

// 渲染修改界面

return $this->fetch();

}

public function DoEdit()

{

// 获取提交数据

$data = Request::param();

// 实例化模型

$sort = new SortModel();

// 修改更新操作

$info = $sort->save([

'title' => $data['title'],

'time' => time(),

'username' => Session::get('username'),

], ['id' => $data['id']]);

// 验证修改结果

if ($info) {

// 返回对应值

return ['res' => 1, 'msg' => '修改成功!'];

} else {

return ['res' => 0, 'msg' => '修改失败!'];

}

}

public function del()

{

// 获取需要删除的分类id

$sortId = Request::param('id');

// 实例化模型

$sort = new SortModel();

// 删除并验证

if ($sort->destroy($sortId)) {

return ['res'=>1,'msg'=>'删除成功!'];

}

}

}{include file="/public/head"}

首页

演示

导航元素

添加

ID分类名管理员创建时间操作

{volist name="sorts" id="sort"}

{$sort.id}

{$sort.title}

{$sort.username}{$sort.time|date="Y-m-d"}

编辑

删除

{/volist}

{$sorts|raw}

layui.use(['form'], function(){

form = layui.form;

form.on('submit(sreach)', function(data){

console.log(data);

$.post("{:url('DoAdd')}",{

'title':$('#title').val()

},function (data) {

if (data.reg == 1){

layer.msg(data.msg,{icon:1,time:1000});

}else{

layer.msg(data.msg,{icon:1,time:1000});

}

})

return false;

})

});

/*用户-删除*/

function member_del(obj,id){

layer.confirm('确认要删除吗?',function(index){

//发异步删除数据

$.get('{:url(\'Sort/del\')}','id='+id,function (data) {

if (data.res == 1){

$(obj).parents("tr").remove();

layer.msg(data.msg,{icon:1,time:1000});

}

})

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值