需求分析:
一件商品可以同時屬于多個擴(kuò)展分類慰丛,搜索任何一個分類都可以搜索出這件商品
第一步 建表
//擴(kuò)展分類表
create table p39_goods_cat(
cat_id? mediumint unsigned not null? comment '分類id',
goods_id? mediumint unsigned not null? comment '商品id',
key goods_id(goods_id),
key cat_id(cat_id)
)engine=InnoDB default charset=utf8 comment '商品的擴(kuò)展分類 ';
第二步 將主分類的代碼復(fù)制到擴(kuò)展分類
<tr>
? ? ? ? ? ? ? ? <td class="label">擴(kuò)展分類:</td>
? ? ? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? ? ? <select name="cat_id">
? ? ? ? ? ? ? ? ? ? <option value="">選擇分類</option>
? ? ? ? ? ? ? ? ? ? <?php foreach ($catData as $k => $v): ?>
? ? ? ? ? ? ? ? ? ? <option value="<?php echo $v['id']; ?>"><?php echo str_repeat('-', 8*$v['level']) . $v['cat_name']; ?></option>
? ? ? ? ? ? ? ? ? ? <?php endforeach; ?>
? ? ? ? ? ? ? ? ? ? </select>
? ? ? ? ? ? ? ? </td>
? ? ? ? ? ? </tr>
如圖所示:
第三步 添加一個按鈕 點(diǎn)擊一次克隆一個擴(kuò)展分類的下拉框
<tr>
? ? ? ? ? ? ? ? <td class="label">擴(kuò)展分類:<input
onclick="$('#cat_list').append($('#cat_list').find('select').eq(0).clone());"
?type="button" id="btn_add_cat" value="添加一個"/></td>
? ? ? ? ? ? ? ? <td>
? ? ? ? ? ? ? ? ? ? <select name="cat_id">
? ? ? ? ? ? ? ? ? ? <option value="">選擇分類</option>
? ? ? ? ? ? ? ? ? ? <?php foreach ($catData as $k => $v): ?>
? ? ? ? ? ? ? ? ? ? <option value="<?php echo $v['id']; ?>"><?php echo str_repeat('-', 8*$v['level']) . $v['cat_name']; ?></option>
? ? ? ? ? ? ? ? ? ? <?php endforeach; ?>
? ? ? ? ? ? ? ? ? ? </select>
? ? ? ? ? ? ? ? </td>
? ? ? ? ? ? </tr>
修改為:
<tr>
? ? ? ? ? ? ? ? <td class="label">擴(kuò)展分類:<input type="button" onclick="$('#cat_list').append($('#cat_list').find('li').eq(0).clone());"? ? id="btn_add_cat" value="添加一個"/></td>
? ? ? ? ? ? ? ? <td >
? ? ? ? ? ? ? ? ? ? <ul? id="cat_list"? >
? ? ? ? ? ? ? ? ? ? ? ? <li>
? ? ? ? ? ? ? ? ? ? <select name="ext_cat_id[]">
? ? ? ? ? ? ? ? ? ? <option value="">選擇分類</option>
? ? ? ? ? ? ? ? ? ? <?php foreach ($catData as $k => $v): ?>
? ? ? ? ? ? ? ? ? ? <option value="<?php echo $v['id']; ?>"><?php echo str_repeat('-', 8*$v['level']) . $v['cat_name']; ?></option>
? ? ? ? ? ? ? ? ? ? <?php endforeach; ?>
? ? ? ? ? ? ? ? ? ? </select>
? ? ? ? ? ? ? ? ? ? ? ? </li>
? ? ? ? ? ? ? ? ? ? </ul>
? ? ? ? ? ? ? ? </td>
? ? ? ? ? ? </tr>
如圖所示:
第四步? 在修改商品模型劫拗,在商品添加之后罢防,獲取商品的ID再接收表單中的擴(kuò)展分類ID數(shù)組插入到分類表
protected function _after_insert($data,$option){
? ? ? ? $ecid=I('post.ext_cat_id');
? ? ? ? if($ecid)
? ? ? ? {
? ? ? ? ? ? $gcModel=D('goods_cat');
? ? ? ? ? ? foreach($ecid as $k=>$v){
? ? ? ? ? ? ? ? if(empty($v))
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? $gcModel->add(array(
? ? ? ? ? ? ? ? ? ? 'cat_id'=>$v,
? ? ? ? ? ? ? ? ? ? 'goods_id'=>$data['id'],
? ? ? ? ? ? ? ? ));
? ? ? ? ? ? }
? ? ? ? }
第五步 在商品列表中再添加一列:“擴(kuò)展分類名稱”
修改商品模型search方法
public function search($perPage=5){
? ? ? ? //取出總的記錄數(shù)
//echo 1;
//echo "<br/>";
? ? ? ? $count=$this->where($where)->count();
? ? ? ? //echo $count;
? ? ? ? ////搜索頁
? ? ? ? $where=array();
? ? ? ? $gn=I('get.gn');
? ? ? ? if($gn) {
? ? ? ? $where['a.goods_name']=array('like',"%$gn%");}//
? ? ? ? $fp=I('get.fp');
? ? ? ? $tp=I('get.tp');
? ? ? ? if($fp&&$tp){
? ? ? ? ? ? $where['a.shop_price']=array('between',array($fp,$tp));
? ? ? ? }
? ? ? ? elseif ($fp) {
? ? ? ? ? ? $where['a.shop_price']=array('egt',$fp);
? ? ? ? }elseif ($tp) {
? ? ? ? ? ? ? $where['a.shop_price']=array('elt',$tp);
? ? ? ? }
? ? ? ? //添加時間
? ? ? ? $fa=I('get.fa');
? ? ? ? $ta=I('get.ta');
? ? ? ? ? if($fa && $ta){
? ? ? ? ? ? $where['a.addtime']=array('between',array($fa,$ta));
? ? ? ? }
? ? ? ? elseif ($fa) {
? ? ? ? ? ? $where['a.addtime']=array('egt',$fa);
? ? ? ? }elseif ($ta) {
? ? ? ? ? ? ? $where['a.addtime']=array('elt',$ta);
? ? ? ? }
? ? ? ? $brandId=I('get.brand_id');
? ? ? ? if($brandId){
? ? ? ? $where['a.brand_id']=array('eq',$brandId);}
? ? ? ? $catId=I('get.cat_id');
? ? ? ? ? if($catId){
? ? ? ? ? ? ? $catModel=D('category');
? ? ? ? ? ? ? $children=$catModel->getChildren($catId);
? ? ? ? ? ? ? $children[]=$catId;
? ? ? ? ? ? $where['a.cat_id']=array('IN',$children);
? ? ? ? ? }
? ? ? ? //是否上架
? ? ? ? $ios=I('get.ios');
? ? ? ? if($ios){
? ? ? ? ? ? $where['a.is_on_sale']=array('eq',$ios);
? ? ? ? }
? ? ? ? //生成翻頁類的對象
? ? ? ? $pageObj=new \Think\Page($count,$perPage);
? ? ? ? ? $pageObj->setConfig('next','下一頁');
? ? ? ? ? ? ? ? ? $pageObj->setConfig('prev','上一頁');
? ? ? ? //生成頁面顯示的上一頁敬扛,下一頁的字符串
? ? ? ? $pageString=$pageObj->show();
? ? ? ? /***************** 排序 *****************/
$orderby = 'a.id';? ? ? // 默認(rèn)的排序字段
$orderway = 'desc';? // 默認(rèn)的排序方式
$odby = I('get.odby');
if($odby)
{
if($odby == 'id_asc')
$orderway = 'asc';
elseif ($odby == 'price_desc')
$orderby = 'shop_price';
elseif ($odby == 'price_asc')
{
$orderby = 'shop_price';
$orderway = 'asc';
}
}
? ? ? ? //取數(shù)據(jù)
? ? ? ? $data=$this->order("$orderby $orderway")
? ? ? ? ? ? ? ->field('a.*,b.brand_name,c.cat_name,e.ext_cat_name')
? ? ? ? ? ? ? ->alias('a')
? ? ? ? ? ? ? // ->join('LEFT JOIN p39_brand b ON a.brand_id=b.id')
? ? ? ? ? ? ? ? ->join('LEFT JOIN __BRAND__ b ON a.brand_id=b.id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __CATEGORY__ c ON a.cat_id=c.id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __GOODS_CAT__ d? ON a.id=d.goods_id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __CATEGORY__ e ON d.cat_id=e.id
')
? ? ? ? ? ? ? ? ->where($where)
? ? ? ? ? ? ? ? ->limit($pageObj->firstRow.','.$pageObj->listRows)
? ? ? ? ? ? ? ? ->select();
? ? ? ? // print_r($data);
? ? ? ? return array(
? ? ? ? ? 'data'=>$data,
? ? ? ? ? ? 'page'=>$pageString,
? ? ? ? );
? ? }
如圖所示:
解決辦法‘:
分組
$data=$this->order("$orderby $orderway")
? ? ? ? ? ? ? ->field('a.*,b.brand_name,c.cat_name,e.cat_name ext_cat_name')
? ? ? ? ? ? ? ->alias('a')
? ? ? ? ? ? ? // ->join('LEFT JOIN p39_brand b ON a.brand_id=b.id')
? ? ? ? ? ? ? ? ->join('LEFT JOIN __BRAND__ b ON a.brand_id=b.id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __CATEGORY__ c ON a.cat_id=c.id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __GOODS_CAT__ d? ON a.id=d.goods_id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __CATEGORY__ e ON d.cat_id=e.id
')
? ? ? ? ? ? ? ? ->where($where)
? ? ? ? ? ? ? ? ->group('a.id')
? ? ? ? ? ? ? ? ->limit($pageObj->firstRow.','.$pageObj->listRows)
? ? ? ? ? ? ? ? ->select();
運(yùn)用一個函數(shù):
GROUP_CONCAT
$data=$this->order("$orderby $orderway")
? ? ? ? ? ? ? ->field('a.*,b.brand_name,c.cat_name,GROUP_CONCAT(e.cat_name) ext_cat_name')
? ? ? ? ? ? ? ->alias('a')
? ? ? ? ? ? ? // ->join('LEFT JOIN p39_brand b ON a.brand_id=b.id')
? ? ? ? ? ? ? ? ->join('LEFT JOIN __BRAND__ b ON a.brand_id=b.id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __CATEGORY__ c ON a.cat_id=c.id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __GOODS_CAT__ d? ON a.id=d.goods_id
? ? ? ? ? ? ? ? ? ? ? ? ? LEFT JOIN __CATEGORY__ e ON d.cat_id=e.id
')
擴(kuò)展:
使用SEPARATOR
->field('a.*,b.brand_name,c.cat_name,GROUP_CONCAT(e.cat_name SEPARATOR "<br/>") ext_cat_name')