ThinkPHP 购物商城网站(数据库中增删改查的功能实现)——————重点——————

时间:2023-03-08 20:57:31
                           控制器   ---------------------GoodsController.class.php-------------------------------------------------
<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller{
public function showlist1(){
// $goods=new GoodsModel();
// $goods=new \Model\GoodsModel();
//实例化model父类对象
$model=D(); //new Model();
var_dump($model);
$this->display();
}
public function showlist(){
$goods=new \Model\GoodsModel();
$info= $goods->order('goods_id desc')->select();
// var_dump($info);
$this->assign('info',$info);
$this->display();
}
public function tianjia(){
//声明一个数组进行添加
// $goods=new \Model\GoodsModel();
// $arr=array(
// 'goods_name'=>'samsungs6',
// 'goods_price'=>4800,
// 'goods_weight'=>130,
// 'goods_number'=>16,
// );
// $z= $goods->add($arr);
// dump($z);
$goods=D('Goods');
if(!empty($_POST)){
//收集表单
// dump($_POST);
$z=$goods->add($_POST);
if($z){
$this->redirect('showlist',, '添加商品成功');
} else {
$this->redirect('tianjia',,'添加商品失败');
}
} else {
//展示表单
$this->display();
} }
public function xiugai($goods_id){
$goods=D('Goods');
if(!empty($_POST)){
$z=$goods->save($_POST);
if($z){
$this->redirect('showlist',array(),, '修改商品成功');
} else {
$this->redirect('xiugai',array('goods_id'=>$goods_id),,'修改商品失败');
}
}else{
$info=$goods->find($goods_id);
$this->assign('info',$info);
$this->display();
}
}
}
                -------------------------Common中Conf的config.php的配置-------------------------------------------------------------
<?php
return array(
//'配置项'=>'配置值'
//smarty 模板引擎切换
// 'TMPL_ENGINE_TYPE' => 'Smarty',
/* 数据库设置 */
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'shop', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '', // 密码
'DB_PORT' => '', // 端口3306
'DB_PREFIX' => 'sw_', // 数据库表前缀
'DB_PARAMS' => array(), // 数据库连接参数
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
'DB_FIELDS_CACHE' => true, // 启用字段缓存
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8 'TMPL_ENGINE_TYPE' => 'Smarty', // 默认模板引擎 以下设置仅对使用Think模板引擎有效
'TMPL_ENGINE_CONFIG' =>array(
// 'left_delimiter' =>'<@@',
// ' right_delimiter ' =>'@@>',
)
);
                                      ————————Model模型中GoodsModel.class.php的配置——————————

<?php
namespace Model;
use Think\Model;
//为goods表创建一个Model模型
//父类model为Think 下的
class GoodsModel extends Model{ }
                              -------Admin(后台)View(视图)中showlist.html页面的显示---------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>会员列表</title> <link href="{$smarty.const.ADMIN_CSS_URL}mine.css" type="text/css" rel="stylesheet" />
</head>
<body>
<style>
.tr_color{ background-color: #9F88FF }
</style>
<div class="div_head">
<span>
<span style="float: left;">当前位置是:商品管理-》商品列表</span>
<span style="float: right; margin-right: 8px; font-weight: bold;">
<a style="text-decoration: none;" href="{$smarty.const.__CONTROLLER__}/tianjia">【添加商品】</a>
</span>
</span>
</div>
<div></div>
<div class="div_search">
<span>
<form action="#" method="get">
品牌<select name="s_product_mark" style="width: 100px;">
<option selected="selected" value="">请选择</option>
<option value="">苹果apple</option>
</select>
<input value="查询" type="submit" />
</form>
</span>
</div>
<div style="font-size: 13px; margin: 10px 5px;">
<table class="table_a" border="" width="100%">
<tbody><tr style="font-weight: bold;">
<td>序号</td>
<td>商品名称</td>
<td>库存</td>
<td>价格</td>
<td>图片</td>
<td>缩略图</td>
<td>品牌</td>
<td>创建时间</td>
<td align="center">操作</td>
</tr>
{foreach $info as $key =>$v}
<tr id="product1">
<td>{$v.goods_id}</td>
<td><a href="#">{$v.goods_name}</a></td>
<td>{$v.goods_number}</td>
<td>{$v.goods_price}</td>
<td><img src="{$smarty.const.ADMIN_IMG_URL}20121018-174034-58977.jpg" height="" width=""></td>
<td><img src="{$smarty.const.ADMIN_IMG_URL}20121018-174034-97960.jpg" height="" width=""></td>
<td>{$v.goods_brand_id}</td>
<td>{$v.goods_create_time|date_format:"%Y-%m-%d %T"}</td>
<td><a href="{$smarty.const.__CONTROLLER__}/xiugai/goods_id/{$v.goods_id}">修改</a></td>
<td><a href="javascript:;" onclick="delete_product(1)">删除</a></td>
</tr>
{/foreach}
<tr>
<td colspan="" style="text-align: center;">
[]
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
                          ----------------------------View中修改页面的实现-----------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>修改商品</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<link href="{$smarty.const.ADMIN_CSS_URL}/mine.css" type="text/css" rel="stylesheet">
</head> <body> <div class="div_head">
<span>
<span style="float:left">当前位置是:商品管理-》修改商品信息</span>
<span style="float:right;margin-right: 8px;font-weight: bold">
<a style="text-decoration: none" href="{$smarty.const.__CONTROLLER__}/showlist">【返回】</a>
</span>
</span>
</div>
<div></div> <div style="font-size: 13px;margin: 10px 5px">
<form action="{$smarty.const.__SELF__}" method="post" enctype="multipart/form-data">
<input type="hidden" name="goods_id" value="{$info.goods_id}"/>
<table border="" width="100%" class="table_a">
<tr>
<td>商品名称</td>
<td><input type="text" name="goods_name" value="{$info.goods_name}" /></td>
</tr>
<tr>
<td>商品分类</td>
<td>
<select name="goods_category_id">
<option>请选择</option>
<option>家用电器</option>
<option>手机数码</option>
<option>电脑办公</option>
<option>服饰鞋帽</option>
</select>
</td>
</tr>
<tr>
<td>商品品牌</td>
<td>
<select name="goods_brand_id">
<option>请选择</option>
<option>苹果</option>
<option>诺基亚</option>
<option>HTC</option>
<option>摩托罗拉</option>
</select>
</td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="goods_price" value="{$info.goods_price}" /></td>
</tr>
<tr>
<td>商品图片</td>
<td><input type="file" name="goods_image" value="./img/2013-12-33.jpg" /></td>
</tr>
<tr>
<td>商品详细描述</td>
<td>
<textarea name="goods_introduce">{$info.goods_introduce}</textarea>
</td>
</tr> <tr>
<td colspan="" align="center">
<input type="submit" value="修改">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
                                ---------------------------View中添加页面的实现--------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>添加商品</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<link href="{$smarty.const.ADMIN_CSS_URL}mine.css" type="text/css" rel="stylesheet">
</head> <body> <div class="div_head">
<span>
<span style="float:left">当前位置是:商品管理-》添加商品信息</span>
<span style="float:right;margin-right: 8px;font-weight: bold">
<a style="text-decoration: none" href="{$smarty.const.__CONTROLLER__}/showlist">【返回】</a>
</span>
</span>
</div>
<div></div> <div style="font-size: 13px;margin: 10px 5px">
<form action="{$smarty.const.__SELF__}" method="post" enctype="multipart/form-data">
<table border="" width="100%" class="table_a">
<tr>
<td>商品名称</td>
<td><input type="text" name="goods_name" /></td>
</tr>
<tr>
<td>商品分类</td>
<td>
<select name="f_goods_category_id">
<option value="">请选择</option>
{foreach from=$s_category_info key=_k item=_v}
<option value="{$_v.category_id}">{$_v.category_name}</option>
{/foreach}
</select>
</td>
</tr>
<tr>
<td>商品品牌</td>
<td>
<select name="f_goods_brand_id">
<option value="">请选择</option>
{foreach from=$s_brand_info key=_k item=_v}
<option value="{$_v.brand_id}">{$_v.brand_name}</option>
{/foreach}
</select>
</td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="f_goods_price" /></td>
</tr>
<tr>
<td>商品图片</td>
<td><input type="file" name="f_goods_image" /></td>
</tr>
<tr>
<td>商品详细描述</td>
<td>
<textarea name="f_goods_introduce"></textarea>
</td>
</tr> <tr>
<td colspan="" align="center">
<input type="submit" value="添加">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

ThinkPHP 购物商城网站(数据库中增删改查的功能实现)——————重点——————图为Netbeans IDE7.2下的购物商城的TP框架结构