1. 程式人生 > >基於ecshop的移動端 etouch實現動態獲取分類商品列表

基於ecshop的移動端 etouch實現動態獲取分類商品列表

修改檔案 category.php

關鍵兩個地方需要修改

    if ($_GET['act'] == 'asynclist') {
        $sayList = array();
        if (is_array($goodslist)) {
            foreach ($goodslist as $vo) {
                $shop_price = empty($vo['promote_price']) ? $vo['shop_price']:$vo['promote_price'];
                $watermark_img
= empty($vo['watermark_img']) ? '':'<img width="55" height="16" src="'.'themes/' . $_CFG['template'].'/images/'.$vo['watermark_img'].'.png" alt="'.$vo['goods_name'].'" />'; /* $sayList[] = array( 'pro-inner' => ' <div class="proImg-wrap"> <a href="'
.$vo['url'].'" > <img src="'.$config['site_url'].$vo['goods_thumb'].'" alt="'.$vo['goods_name'].'"> </a> </div> <div class="proInfo-wrap"> <div class="proTitle"> <a href="'.$vo['url'].'" >'.$vo['name'].'</a> </div> <div class="proSKU"></div> <div class="proPrice"> <em>'
. $shop_price .'</em> </div> <div class="proService"> <del>'.$vo['market_price'].'</del></div> <div class="proSales">月銷:<em>'.$vo['sales_count'].'</em></div> <div class="proIcons">'.$watermark_img.'</div> </div>' );*/ $collectStr = ''; if($vo['collect']){ //如果已經蒐藏了那麼 $collectStr = '<a href="javascript://" onclick="uncollect(this,'.$vo['goods_id'].')" class="ycd-font-icon uncollect">&#xe63e;</a>'; }else{ //如果沒有蒐藏那麼 $collectStr = '<a href="javascript://" onclick="collect(this,'.$vo['goods_id'].')" class="ycd-font-icon collect">&#xe620;</a>'; } $sayList[] = array( 'pro-inner'=>' <a href="'.$vo['url'].'" class="img"> <img src="'.$config['site_url'].$vo['goods_thumb'].'" alt="'.$vo['goods_name'].'"> </a> <div class="cart-price"> <span class="name"> '.$vo['name'].' </span> <p class="price"> <span class="price-p">'. $shop_price .'</span> </p> <p class="more"> 月銷:'.$vo['sales_count'].' </p> </div> '.$collectStr.' <div class="cart-operation"> <!--<span class="ycd-font-icon cut" onclick="goods_cut(this);changePrice(this);" data-operation="pre" data-goods-id="'.$vo['goods_id'].'">&#xe62a;</span>--> <input type="number" class="amount" id="number" onblur="changePrice(this);" name="number" value="1" min="1" max="1" style="display:none;"/> <span class="ycd-font-icon increse" onclick="changePrice(this);" data-operation="next" data-goods-id="'.$vo['goods_id'].'">&#xe62d;</span> </div> ' ); } } echo json_encode($sayList); exit; }

還有一個叫做 category_get_goods 這個函式此函式新增了一個欄位 collect返回欄位可以用於前端頁面檢視渲染的時候實現判斷是否已經蒐藏 並且根據是否蒐藏顯示不同的樣式和繫結不同的事件函式

/**
 * 獲得分類下的商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order)
{
    $display = $GLOBALS['display'];
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    if ($brand > 0)
    {
        $where .=  "AND g.brand_id=$brand ";
    }

    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }

    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }

    /* 獲得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .
                "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
                'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . ' AS mp ' .
                "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
            "WHERE $where $ext ORDER BY $sort $order";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        /* 處理商品水印圖片 */
        $watermark_img = '';

        if ($promote_price != 0)
        {
            $watermark_img = "watermark_promote_small";
        }
        elseif ($row['is_new'] != 0)
        {
            $watermark_img = "watermark_new_small";
        }
        elseif ($row['is_best'] != 0)
        {
            $watermark_img = "watermark_best_small";
        }
        elseif ($row['is_hot'] != 0)
        {
            $watermark_img = 'watermark_hot_small';
        }

        if ($watermark_img != '')
        {
            $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
        }

        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        if($display == 'grid')
        {
            $arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        }
        else
        {
            $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        }
        $arr[$row['goods_id']]['name']             = $row['goods_name'];
        $arr[$row['goods_id']]['goods_brief']      = $row['goods_brief'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['sales_count']      = get_sales_volume($row['goods_id']); //顯示月銷量 by wang
        if (isset($_SESSION['user_id']) || $_SESSION['user_id'] != 0)
        { 
            /* 檢查是否已經存在於使用者的收藏夾 */
            $sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('collect_goods') .
                " WHERE user_id='$_SESSION[user_id]' AND goods_id = '".$row['goods_id']."'";

            if ($GLOBALS['db']->GetOne($sql) > 0)
            {

                $arr[$row['goods_id']]['collect'] = 1;
            }
            else
            {
                $arr[$row['goods_id']]['collect'] = 0;
            }

        }else{
            $arr[$row['goods_id']]['collect'] = 0;
        }


    }

    return $arr;
}

客戶端js程式碼參考


/* *

 * 新增商品到收藏夾

 */
var _current_collect = null;
var _current_goodsId = 0;

function collect(_this,goodsId)
{
  _current_collect = _this;
  _current_goodsId = goodsId;
  jQuery.get('user.php?act=collect',{id:goodsId},collectResponse,"JSON");
}

function uncollect(_this,goodsId)
{
  _current_collect = _this;
  _current_goodsId = goodsId;
  jQuery.get('user.php?act=uncollect',{id:goodsId},collectResponse,"JSON");
}


/* *

 * 處理收藏商品的反饋資訊

 */

function collectResponse(result)
{

     if(parseInt(result.error) === 0){
         _current_collect.className = '';
         _current_collect.className ="ycd-font-icon uncollect";
         _current_collect.setAttribute('onclick',"uncollect(this,"+_current_goodsId+")");
         _current_collect.innerHTML = '&#xe63e;';
     }
     if(parseInt(result.error) === 3){

         _current_collect.className = '';
         _current_collect.className ="ycd-font-icon collect";
         _current_collect.setAttribute('onclick',"collect(this,"+_current_goodsId+")");
         _current_collect.innerHTML = '&#xe620;';
     }
    //這裡加入成功
    //注意這裡引入了一個自己寫的基於zepto移動端函式庫的一個外掛 請參考我以前寫的一篇文章裡面有這個外掛的程式碼
    Zepto('body').popup({
            title:'提示資訊'
            ,id:'alert'
            ,closeOnOk:true
            ,ok:'確定',
            message:result.message
        }); 

}

總結

以前非常喜歡php 弄過一段時間 所以php程式碼也是基本上都能夠看懂 mysql 也學過
沒有php 基礎的童鞋可以先去學學php基礎
現在博主努力專研前端 後端也在學習 最近學習javascript 和 html5的新api 這些
記錄下自己的工作