基于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 这些
记录下自己的工作

你可能感兴趣的:(PHP)