您的位置:首页 > 编程语言 > PHP开发

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"}>

效果如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php