TP電商項(xiàng)目:商品擴(kuò)展類的添加

需求分析:

一件商品可以同時(shí)屬于多個(gè)擴(kuò)展分類,搜索任何一個(gè)分類都可以搜索出這件商品

第一步 建表

//擴(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>

如圖所示:


第三步 添加一個(gè)按鈕 點(diǎn)擊一次克隆一個(gè)擴(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="添加一個(gè)"/></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="添加一個(gè)"/></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);

? ? ? ? }

? ? ? ? //添加時(shí)間

? ? ? ? $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);

? ? ? ? }


? ? ? ? //生成翻頁類的對(duì)象

? ? ? ? $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)用一個(gè)函數(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')


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容