brophp写商品管理模块
2015-07-18 22:42
585 查看
学了几天的brophp,感觉自己还是有点晕,不知道什么功能应该写到哪个模块里面,现在先学着,等看完视频回过头来总结一下。
首先在control下面的goods.class.php下写:
<?php
class Goods {
//查询
function index() {
$goods = D('goods');
$page = new Page($goods->total(), 10);
$this->assign('goods', $goods->field('id, name, price, num, ptime')->limit($page->limit)->select());
$this->assign('fpage', $page->fpage());
$this->assign('page', $page->page);
$this->display();
}
//要添加界面
function add() {
$this->display();
}
//加入数据库
function insert() {
$goods = D("goods");
$_POST['ptime'] = time();
$up = upload();
if($up[0]) {
$_POST['pic'] = $up[1];
} else {
$this->error($up[1], 3, "add");
}
if($goods->insert($_POST, 1, 1)) {
$this->success("添加商品成功!", 1, "index");
} else {
if($up[0]) {
delpic($up[1]);
}
$this->error($goods->getMsg(), 3, "add");
}
}
//要修改界面
function mod() {
$this->assign(D("goods")->find($_GET['id']));
$this->display();
}
//向数据库中修改
function update() {
$goods = D("goods");
if($_FILES['uppic']['error']==0) {
$up = upload();
if($up[0]) {
$_POST['pic'] = $up[1];
} else {
$this->error($up[1], 3, "add");
}
}
if($goods -> update($_POST, 1, 1)) {
if($up[0]) {
delpic($_POST['dpic']);
}
$this->success("修改成功!", 1, "index");
} else {
if($up[0]) {
delpic($up[1]);
}
$this->error($goods->getMsg(), 3, "goods/mod/id/{$_POST['id']}");
}
}
//删除
function del() {
$goods = D("goods");
// p($_GET['id'], $_POST['id']);
$id = !empty($_POST['id']) ? $_POST['id'] : $_GET['id'];
$filenames = $goods->field('pic')->where($id)->select();
if($goods->where($id)->delete()) {
delmulpic($filenames);
$this->redirect('index', "page/{$_GET['page']}");
} else {
$this -> error("删除商品失败!", 3, "index");
}
}
}
view index.html下面代码如下:
<{include "public/header.tpl"}>
<table border="1" width="800" align="center">
<form action="<{$url}>/del" method="post" onsubmit="return confirm('你确定要删除这些商品吗?')">
<caption><h1>商品列表</h1></caption>
<tr>
<th> </th> <th>ID</th> <th>商品名称</th> <th>商品价格</th><th>数量</th><th>添加时间</th><th>操作</th>
</tr>
<{foreach $goods as $good}>
<tr>
<td><input type="checkbox" name="id[]" value="<{$good.id}>"></td>
<td><{$good.id}></td>
<td><{$good.name}></td>
<td>¥<{$good.price}></td>
<td><{$good.num}></td>
<td><{$good.ptime|date_format:"%Y-%m-%d"}></td>
<td><a href="<{$url}>/mod/id/<{$good.id}>">修改</a>/<a onclick="return confirm('你确定要删除<{$good.name}>吗?')" href="<{$url}>/del/id/<{$good.id}>/page/<{$page}>">删除</a></td>
</tr>
<{foreachelse}>
<tr> <td colspan="7">没有商品 </td></tr>
<{/foreach}>
<tr>
<td><input type="submit" name="del" value="删除"></td> <td colspan="6" align="right"><{$fpage}></td>
</tr>
</form>
</table>
<{include "public/footer.tpl"}>
add.html代码如下:
<{include "public/header.html"}>
<h1>添加商品</h1>
<form action="<{$url}>/insert" method="post">
商品名称:<input type="text" name="name" value=""><br>
商品价格:<input type="text" name="price" value=""><br>
商品数量:<input type="text" name="num" value=""><br>
商品图片:<input type="file" name="uppic" value=""><br>
商品介绍:<textarea name="desn" cols="40" rows="8"></textarea><br>
<input type="submit" name="submit" value="提交">
</form>
<{include "public/footer.html"}>mod.html中代码如下:
<{include "public/header.html"}>
<h1>修改商品</h1>
<form action="<{$url}>/update" method="post">
<input type="hidden" name="id" value="<{$id}>">
商品名称:<input type="text" name="name" value="<{$name}>"><br>
商品价格:<input type="text" name="price" value="<{$price}>"><br>
商品数量:<input type="text" name="num" value="<{$num}>"><br>
商品图片:<input type="file" name="uppic" value=""><br>
商品介绍:<textarea name="desn" cols="40" rows="8"><{$desn}></textarea> <br>
<input type="submit" name="submit" value="修改商品">
</form>
<{include "public/footer.html"}>
效果如下:
首先在control下面的goods.class.php下写:
<?php
class Goods {
//查询
function index() {
$goods = D('goods');
$page = new Page($goods->total(), 10);
$this->assign('goods', $goods->field('id, name, price, num, ptime')->limit($page->limit)->select());
$this->assign('fpage', $page->fpage());
$this->assign('page', $page->page);
$this->display();
}
//要添加界面
function add() {
$this->display();
}
//加入数据库
function insert() {
$goods = D("goods");
$_POST['ptime'] = time();
$up = upload();
if($up[0]) {
$_POST['pic'] = $up[1];
} else {
$this->error($up[1], 3, "add");
}
if($goods->insert($_POST, 1, 1)) {
$this->success("添加商品成功!", 1, "index");
} else {
if($up[0]) {
delpic($up[1]);
}
$this->error($goods->getMsg(), 3, "add");
}
}
//要修改界面
function mod() {
$this->assign(D("goods")->find($_GET['id']));
$this->display();
}
//向数据库中修改
function update() {
$goods = D("goods");
if($_FILES['uppic']['error']==0) {
$up = upload();
if($up[0]) {
$_POST['pic'] = $up[1];
} else {
$this->error($up[1], 3, "add");
}
}
if($goods -> update($_POST, 1, 1)) {
if($up[0]) {
delpic($_POST['dpic']);
}
$this->success("修改成功!", 1, "index");
} else {
if($up[0]) {
delpic($up[1]);
}
$this->error($goods->getMsg(), 3, "goods/mod/id/{$_POST['id']}");
}
}
//删除
function del() {
$goods = D("goods");
// p($_GET['id'], $_POST['id']);
$id = !empty($_POST['id']) ? $_POST['id'] : $_GET['id'];
$filenames = $goods->field('pic')->where($id)->select();
if($goods->where($id)->delete()) {
delmulpic($filenames);
$this->redirect('index', "page/{$_GET['page']}");
} else {
$this -> error("删除商品失败!", 3, "index");
}
}
}
view index.html下面代码如下:
<{include "public/header.tpl"}>
<table border="1" width="800" align="center">
<form action="<{$url}>/del" method="post" onsubmit="return confirm('你确定要删除这些商品吗?')">
<caption><h1>商品列表</h1></caption>
<tr>
<th> </th> <th>ID</th> <th>商品名称</th> <th>商品价格</th><th>数量</th><th>添加时间</th><th>操作</th>
</tr>
<{foreach $goods as $good}>
<tr>
<td><input type="checkbox" name="id[]" value="<{$good.id}>"></td>
<td><{$good.id}></td>
<td><{$good.name}></td>
<td>¥<{$good.price}></td>
<td><{$good.num}></td>
<td><{$good.ptime|date_format:"%Y-%m-%d"}></td>
<td><a href="<{$url}>/mod/id/<{$good.id}>">修改</a>/<a onclick="return confirm('你确定要删除<{$good.name}>吗?')" href="<{$url}>/del/id/<{$good.id}>/page/<{$page}>">删除</a></td>
</tr>
<{foreachelse}>
<tr> <td colspan="7">没有商品 </td></tr>
<{/foreach}>
<tr>
<td><input type="submit" name="del" value="删除"></td> <td colspan="6" align="right"><{$fpage}></td>
</tr>
</form>
</table>
<{include "public/footer.tpl"}>
add.html代码如下:
<{include "public/header.html"}>
<h1>添加商品</h1>
<form action="<{$url}>/insert" method="post">
商品名称:<input type="text" name="name" value=""><br>
商品价格:<input type="text" name="price" value=""><br>
商品数量:<input type="text" name="num" value=""><br>
商品图片:<input type="file" name="uppic" value=""><br>
商品介绍:<textarea name="desn" cols="40" rows="8"></textarea><br>
<input type="submit" name="submit" value="提交">
</form>
<{include "public/footer.html"}>mod.html中代码如下:
<{include "public/header.html"}>
<h1>修改商品</h1>
<form action="<{$url}>/update" method="post">
<input type="hidden" name="id" value="<{$id}>">
商品名称:<input type="text" name="name" value="<{$name}>"><br>
商品价格:<input type="text" name="price" value="<{$price}>"><br>
商品数量:<input type="text" name="num" value="<{$num}>"><br>
商品图片:<input type="file" name="uppic" value=""><br>
商品介绍:<textarea name="desn" cols="40" rows="8"><{$desn}></textarea> <br>
<input type="submit" name="submit" value="修改商品">
</form>
<{include "public/footer.html"}>
效果如下:
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- C语言实现的统计php代码行数功能源码(支持文件夹、多目录)
- php xml 入门学习资料