tp基本的增删改查的操作demo

1、controller

<?php
/**
 * Created by PhpStorm.
 * User: Louis
 * Date: 2015/9/10
 * Time: 21:58
 */

namespace Admin\Controller;


use Think\Controller;

class BaseController extends Controller
{

    protected $model;
    protected $meta_title = '功能标题';

    public function index()
    {

        //搜索
        $keyword = I('get.keyword');
        $wheres = array();
        if (!empty($keyword)) {
            $wheres['name'] = array('like', "%$keyword%");
        }

        $pageResult = $this->model->page($wheres);
        $this->assign($pageResult);
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->assign('meta_title',$this->meta_title);
        $this->display();
    }

    public function add()
    {
        if (IS_POST) {
            if ($this->model->create() !== false) {
                if ($this->model->add() !== false) {
                    $this->success('保存成功!', U('index'));
                    exit;

                }
                $this->error('保存失败!!' . $this->model->getError());
            }
            $this->error('数据收集失败!' . $this->model->getError());
        }
        $this->assign('edit','添加');
        $this->assign('meta_title',$this->meta_title);
        $this->display('edit');
    }

    public function changeStatus($id, $status)
    {

        $result = $this->model->changeStatus($id, $status);
        if ($result !== false) {
            $this->success('操作成功', cookie('__forward__'));
        } else {
            $this->error('操作失败');
        }
    }

    public function edit($id)
    {
        if (IS_POST) {
            if ($this->model->create() !== false) {
                if ($this->model->save() !== false) {
                    $this->success('更新成功', cookie('__forward__'));
                    exit;
                }
            }
            $this->error('更新失败');
        } else {
            $row = $this->model->find($id);
            $this->assign('edit','编辑');
            $this->assign('meta_title',$this->meta_title);
            $this->assign($row);
            $this->display();
        }

    }
}
2、model

<?php
/**
 * Created by PhpStorm.
 * User: Louis
 * Date: 2015/9/10
 * Time: 22:01
 */

namespace Admin\Model;


use Think\Model;
use Think\Page;

class BaseModel extends Model
{

    public function changeStatus($id, $status)
    {
        $this->data['id'] = $id;
        $this->data['status'] = $status;
        if ($status == -1) {
            $this->data['name'] = array('exp', 'concat(name,"_del")');
        }
        return parent::save();
    }

    public function page($wheres)
    {
        //准备分页工具条和当前页数据rows
        if(in_array('status',$this->getDbFields())){
            $wheres['status'] = array('EGT',0);
        }
        $count = $this->where($wheres)->where('status>=0')->count();
        $page = new Page($count, C('PAGESIZE'));
        $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $pageToolHTML = $page->show();
        $start = $page->firstRow;
        if ($page->firstRow >= $page->totalRows) {
            $start = $page->totalRows - $page->listRows;
        }
        $orders = array();
        if(in_array('status',$this->getDbFields())){
            $orders['status'] = 'desc';
        }
        if(in_array('sort',$this->getDbFields())){
            $orders['sort'] = 'desc';
        }
        $rows = $this->where($wheres)->where('status>=0')->order($orders)->limit($start, $page->listRows)->select();

        return array('pageToolHTML' => $pageToolHTML, 'rows' => $rows);
    }
}
3、index

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 商品{$meta_title} </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="__CSS__/general.css" rel="stylesheet" type="text/css" />
<link href="__CSS__/main.css" rel="stylesheet" type="text/css" />
<link href="__CSS__/page.css" rel="stylesheet" type="text/css" />
    <block name="css"></block>
</head>
<body>
<h1>
    <span class="action-span"><a href="{:U('add')}">添加{$meta_title}</a></span>
    <span class="action-span1"><a href="#">商城 管理中心</a></span>
    <span id="search_id" class="action-span1"> - 商品{$meta_title} </span>
    <div style="clear:both"></div>
</h1>
<div class="form-div">
    <block name="search">
        <form action="{:U('index')}" name="searchForm">
            <img src="__IMG__/icon_search.gif" width="26" height="22" border="0" alt="search" />
            <input type="text" name="keyword" size="15" value="{$Think.get.keyword}" />
            <input type="submit" value=" 搜索 " class="button" />
        </form>
    </block>
</div>
<block name="list"></block>
<div id="footer">
共执行 3 个查询,用时 0.021251 秒,Gzip 已禁用,内存占用 2.194 MB<br />
版权所有 &copy; 2005-2012 上海商派网络科技有限公司,并保留所有权利。</div>
<block name="js"></block>
</body>
</html>
4、edit

<!-- $Id: brand_info.htm 14216 2008-03-10 02:27:21Z testyang $ -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ECSHOP 管理中心 - {$edit}{$meta_title} </title>
    <meta name="robots" content="noindex, nofollow">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="__CSS__/general.css" rel="stylesheet" type="text/css" />
    <link href="__CSS__/main.css" rel="stylesheet" type="text/css" />
    <block name="css"></block>
</head>
<body>
<h1>
    <span class="action-span"><a href="{:U('index')}">商品{$meta_title}</a></span>
    <span class="action-span1"><a href="#">商城 管理中心</a></span>
    <span id="search_id" class="action-span1"> - {$edit}{$meta_title} </span>
    <div style="clear:both"></div>
</h1>
<div class="main-div">
    <block name="form">

    </block>

</div>

<div id="footer">
    共执行 1 个查询,用时 0.018952 秒,Gzip 已禁用,内存占用 2.197 MB<br />
    版权所有 &copy; 2005-2012 上海商派网络科技有限公司,并保留所有权利。</div>
</body>
<script type="text/javascript" src="__JS__/jquery-1.11.1.js"></script>
<block name="js">
</block>

</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值