Maison  >  Article  >  développement back-end  >  基于ecshop的移动端 etouch实现动态获取分类商品列表

基于ecshop的移动端 etouch实现动态获取分类商品列表

WBOY
WBOYoriginal
2016-07-29 08:57:051547parcourir

修改文件 category.php

关键两个地方需要修改

<code><span>if</span> (<span>$_GET</span>[<span>'act'</span>] == <span>'asynclist'</span>) {
        <span>$sayList</span> = array();
        <span>if</span> (is_array(<span>$goodslist</span>)) {
            foreach (<span>$goodslist</span> as <span>$vo</span>) {
                <span>$shop_price</span> = empty(<span>$vo</span>[<span>'promote_price'</span>]) ? <span>$vo</span>[<span>'shop_price'</span>]:<span>$vo</span>[<span>'promote_price'</span>];
                <span>$watermark_img</span> = empty(<span>$vo</span>[<span>'watermark_img'</span>]) ? <span>''</span>:<span>'<img    style="max-width:90%"  style="max-width:90%" src="'</span>.<span>'themes/'</span>%20.%20<span>%24_CFG</span>%5B<span>'template'</span>%5D.<span>'/images/'</span>.<span>%24vo</span>%5B<span>'watermark_img'</span>%5D.<span>'.png" alt="基于ecshop的移动端 etouch实现动态获取分类商品列表">.<span>$vo</span>[<span>'goods_name'</span>].<span>'" />'</span>;
               /* <span>$sayList</span>[] = array(
                    <span>'pro-inner'</span> => <span>'
        <div> <a href="'</span>.<span>%24vo</span>%5B<span>'url'</span>%5D.<span>'"> <img src="'</span>.<span>%24config</span>%5B<span>'site_url'</span>%5D.<span>%24vo</span>%5B<span>'goods_thumb'</span>%5D.<span>'" alt="基于ecshop的移动端 etouch实现动态获取分类商品列表">.<span>$vo</span>[<span>'goods_name'</span>].<span>'"> </span></a> </div>
        <div>
          <div> <a href="'</span>.<span>%24vo</span>%5B<span>'url'</span>%5D.<span>'">'.<span>$vo</span>[<span>'name'</span>].<span>'</span></a> </div>
          <div></div>
          <div>
            <em>'. <span>$shop_price</span> .<span>'</span></em> 
          </div>
          <div> <del>'.<span>$vo</span>[<span>'market_price'</span>].<span>'</span></del>
</div>
          <div>月销:<em>'.<span>$vo</span>[<span>'sales_count'</span>].<span>'</span></em>
</div>
          <div>'.<span>$watermark_img</span>.<span>'</span>
</div>
        </div>'</span>
                );*/
                <span>$collectStr</span> = <span>''</span>;
                <span>if</span>(<span>$vo</span>[<span>'collect'</span>]){
                    //如果已经搜藏了那么
                    <span>$collectStr</span> = <span>'<a>$vo</a></span>[<span>'goods_id'</span>].<span>')">'</span>;
                }<span>else</span>{
                    //如果没有搜藏那么
                    <span>$collectStr</span> = <span>'<a>$vo</a></span>[<span>'goods_id'</span>].<span>')">'</span>;
                }
                <span>$sayList</span>[] = array(
                 <span>'pro-inner'</span>=><span>'
                     <a href="'</span>.<span>%24vo</span>%5B<span>'url'</span>%5D.<span>'">
                        <img src="'</span>.<span>%24config</span>%5B<span>'site_url'</span>%5D.<span>%24vo</span>%5B<span>'goods_thumb'</span>%5D.<span>'" alt="基于ecshop的移动端 etouch实现动态获取分类商品列表">.<span>$vo</span>[<span>'goods_name'</span>].<span>'">
                    </span></a>
                    <div>
                        <span>
                            '</span>.<span>$vo</span>[<span>'name'</span>].<span>'
                        </span>
                        <p>
                            <span>'</span>. <span>$shop_price</span> .<span>'</span>
                        </p>
                        <p>
                            月销:'.<span>$vo</span>[<span>'sales_count'</span>].<span>'
                        </span></p>
                    </div>
                    '</span>.<span>$collectStr</span>.<span>'
                    <div>
                         <!--<span  data-operation="pre" data-goods-id="'</span>.<span>$vo</span>[<span>'goods_id'</span>].<span>'">&#xe62a;</span>-->
                         <input type="number" name="number" value="1" min="1" max="1">
                         <span data-operation="next" data-goods-id="'</span>.<span>$vo</span>[<span>'goods_id'</span>].<span>'"></span>
                    </div>
                 '</span>
                );
            }
        }
        <span>echo</span> json_encode(<span>$sayList</span>);
        <span>exit</span>;
    }</span></code>

还有一个叫做 category_get_goods 这个函数此函数新增了一个字段 collect返回字段可以用于前端页面视图渲染的时候实现判断是否已经搜藏 并且根据是否搜藏显示不同的样式和绑定不同的事件函数

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

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

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

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

    <span>/* 获得商品列表 */</span><span>$sql</span> = <span>'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, '</span> .
                <span>"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, "</span> .
                <span>'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img '</span> .
            <span>'FROM '</span> . <span>$GLOBALS</span>[<span>'ecs'</span>]->table(<span>'goods'</span>) . <span>' AS g '</span> .
            <span>'LEFT JOIN '</span> . <span>$GLOBALS</span>[<span>'ecs'</span>]->table(<span>'member_price'</span>) . <span>' AS mp '</span> .
                <span>"ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' "</span> .
            <span>"WHERE $where $ext ORDER BY $sort $order"</span>;
    <span>$res</span> = <span>$GLOBALS</span>[<span>'db'</span>]->selectLimit(<span>$sql</span>, <span>$size</span>, (<span>$page</span> - <span>1</span>) * <span>$size</span>);

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

        <span>/* 处理商品水印图片 */</span><span>$watermark_img</span> = <span>''</span>;

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

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

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

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

                <span>$arr</span>[<span>$row</span>[<span>'goods_id'</span>]][<span>'collect'</span>] = <span>1</span>;
            }
            <span>else</span>
            {
                <span>$arr</span>[<span>$row</span>[<span>'goods_id'</span>]][<span>'collect'</span>] = <span>0</span>;
            }

        }<span>else</span>{
            <span>$arr</span>[<span>$row</span>[<span>'goods_id'</span>]][<span>'collect'</span>] = <span>0</span>;
        }


    }

    <span>return</span><span>$arr</span>;
}</span></code>

客户端js代码参考

<code><span>/* *

 * 添加商品到收藏夹

 */</span><span>var</span> _current_collect = <span>null</span>;
<span>var</span> _current_goodsId = <span>0</span>;

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

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


<span>/* *

 * 处理收藏商品的反馈信息

 */</span><span><span>function</span><span>collectResponse</span><span>(result)</span>
{</span><span>if</span>(<span>parseInt</span>(result.error) === <span>0</span>){
         _current_collect.className = <span>''</span>;
         _current_collect.className =<span>"ycd-font-icon uncollect"</span>;
         _current_collect.setAttribute(<span>'onclick'</span>,<span>"uncollect(this,"</span>+_current_goodsId+<span>")"</span>);
         _current_collect.innerHTML = <span>''</span>;
     }
     <span>if</span>(<span>parseInt</span>(result.error) === <span>3</span>){

         _current_collect.className = <span>''</span>;
         _current_collect.className =<span>"ycd-font-icon collect"</span>;
         _current_collect.setAttribute(<span>'onclick'</span>,<span>"collect(this,"</span>+_current_goodsId+<span>")"</span>);
         _current_collect.innerHTML = <span>''</span>;
     }
    <span>//这里加入成功</span><span>//注意这里引入了一个自己写的基于zepto移动端函数库的一个插件 请参考我以前写的一篇文章里面有这个插件的代码</span>
    Zepto(<span>'body'</span>).popup({
            title:<span>'提示信息'</span>
            ,id:<span>'alert'</span>
            ,closeOnOk:<span>true</span>
            ,ok:<span>'确定'</span>,
            message:result.message
        }); 

}</code>

总结

以前非常喜欢php 弄过一段时间 所以php代码也是基本上都能够看懂 mysql 也学过
没有php 基础的童鞋可以先去学学php基础
现在博主努力专研前端 后端也在学习 最近学习javascript 和 html5的新api 这些
记录下自己的工作

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了基于ecshop的移动端 etouch实现动态获取分类商品列表,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:PHP学习笔记-PHP语言基础3Article suivant:Nginx-配置误区