tp基本的增删改查的操作demo
1、controller
<?php /** * Created by PhpStorm. * User: Louis * Date: 2015/9/10 * Time: 21:58 */ namespace AdminController; use ThinkController; 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 AdminModel; use ThinkModel; use ThinkPage; 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 /> 版权所有 © 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 /> 版权所有 © 2005-2012 上海商派网络科技有限公司,并保留所有权利。</div> </body> <script type="text/javascript" src="__JS__/jquery-1.11.1.js"></script> <block name="js"> </block> </html>
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: tp5源码分析之数据库查询
- 下一篇: 写了一个内存地址特征码搜索工具