ecshop商品列表页,循环显示当前分类的二级分类以及分类下的商品

1.includes\lib_goods.php,在最末尾添加几个function

/**
 * 获得指定分类下的子分类
 *
 * @access  public
 * @param   integer     $cat_id     分类编号
 * @return  array
 */
function get_children_tree($cat_id)
{
	if ($cat_id >0 )
	{
		$sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id'";
        if ($GLOBALS['db']->getOne($sql))
        {
			// 获取当前分类名及其子类
			$sql = 'SELECT a.cat_id, a.cat_name, a.cat_desc, a.sort_order AS parent_order, a.cat_id, ' .
				   'b.cat_id AS child_id, b.cat_name AS child_name, b.cat_desc AS child_desc, b.sort_order AS child_order ' .
				   'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
				   'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id ' .
				   "WHERE a.cat_id = '$cat_id' ORDER BY a.cat_id ASC,parent_order ASC, child_order ASC";
		}
		else
		{
			$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
			$parent_id = $GLOBALS['db']->getOne($sql);
			if ($parent_id > 0)
			{
				//获取当前分类、兄弟及其父类
 				$sql = 'SELECT a.cat_id, a.cat_name, a.cat_desc, b.cat_id AS child_id, b.cat_name AS child_name, b.cat_desc AS child_desc, b.sort_order ' .
					   'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
					   'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id ' .
					   "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC";
			}
			else
			{
				//获取当前分类
				$sql = 'SELECT a.cat_id, a.cat_name FROM '
					   . $GLOBALS['ecs']->table('category') . ' AS a ' .
					   "WHERE a.cat_id = '$cat_id'";
            }
        }
        $res = $GLOBALS['db']->getAll($sql);
		$cat_arr = array();
	    foreach ($res AS $row)
		{
			$cat_arr[$row['cat_id']]['id']   = $row['cat_id'];
			$cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
			$cat_arr[$row['cat_id']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
			$cat_arr[$row['cat_id']]['cat_desc'] = $row['cat_desc'];

			if ($row['child_id'] != NULL){
				$cat_arr[$row['cat_id']]['children'][$row['child_id']]['id']   = $row['child_id'];
				$cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name'];
				$cat_arr[$row['cat_id']]['children'][$row['child_id']]['url']  = build_uri('category', array('cid' => $row['child_id']), $row['child_name']);
				$cat_arr[$row['cat_id']]['children'][$row['child_id']]['cat_desc'] = $row['child_desc'];
			}
		}
    return $cat_arr;
    }
}

/**
 * 获取指定分类产品
 *
 * @access      public
 * @param       string      $cat_id(num)_best_goods
 * @param       array       $cat_id(num)_best_goods
 * @return      array
 */
function get_cat_id_goods_list($cat_id = '', $num = '') {
	$sql = 'Select g.goods_id, g.cat_id,c.parent_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, g.sales_volume_base, g.is_shipping, ' .
		   "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
		   "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, " .
		   "g.is_best, g.is_new, g.is_hot, g.is_promote " .
		   'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
		   'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON c.cat_id = g.cat_id ' .
		   "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
		   "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
		   "Where g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ";
	$sql .= " AND (c.parent_id =" . $cat_id. " OR g.cat_id = " . $cat_id ." OR g.cat_id ". db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) .")";
	$sql .= "ORDER BY g.sales_volume_base DESC LIMIT $num";
	$res = $GLOBALS['db']->getAll($sql);
	$goods = array();

	foreach ($res AS $idx => $row)
	{
		$goods[$idx]['id'] = $row['goods_id'];
		$goods[$idx]['name'] = $row['goods_name'];
		$goods[$idx]['brief'] = $row['goods_brief'];
		$goods[$idx]['sales_volume_base'] = $row['sales_volume_base'];
		$goods[$idx]['is_shipping'] = $row['is_shipping'];
		$goods[$idx]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
		$goods[$idx]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
		sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
		$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
		$goods[$idx]['market_price'] = price_format($row['market_price']);
		$goods[$idx]['promote_price'] = price_format($row['promote_price']);
		$goods[$idx]['shop_price'] = price_format($row['shop_price']);
		$goods[$idx]['thumb'] = empty($row['goods_thumb']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_thumb'];
		$goods[$idx]['goods_img'] = empty($row['goods_img']) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_img'];
		$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
	}
	return $goods;
}

/**
 * 获取指定分类产品的相册
 *
 * @access      public
 * @param       string      $cat_idnum_best_goods
 * @param       array       $cat_idnum_best_goods
 * @return      array
 */
function get_thumb($goods_id){
	$sql = "SELECT img_url,thumb_url FROM " . $GLOBALS['ecs']->table('goods_gallery') ." WHERE goods_id = ".$goods_id;
	$res = $GLOBALS['db']->getAll($sql);
	return $res;
}

2.category.php,在

$smarty->assign('feed_url',         ($_CFG['rewrite'] == 1) ? "feed-c$cat_id.xml" : 'feed.php?cat=' . $cat_id); // RSS URL

后面,添加

    /********************************************************************************************/
    //在分类页面调用当前分类以及其二级分类的信心,再在每个分类下调用5个商品
    $cats = get_children_tree($cat_id);//获取当前分类信息以及子分类信息
    $cat_detail = array();
    foreach ($cats as $key => $val)
    {
        foreach($cats[$key]['children'] as $_k =>$_v){
            $cats[$key]['children'][$_k]['goods_detail'] = get_cat_id_goods_list($_v['id'],5);
            foreach($cats[$key]['children'][$_k]['goods_detail'] as $_ke =>$_va){
                $cats[$key]['children'][$_k]['goods_detail'][$_ke]['thumb_url'] = get_thumb($_va['id']);
            }
        }
        $cat_detail = $cats[$key]['children'];//当前分类下的二级分类及其商品信息
    }
    $smarty->assign('cat_detail',      $cat_detail);
    /********************************************************************************************/
<!--循环二级分类-->
<!--{foreach from=$cat_detail item=cat name=cats_detail}-->
<div id=content class="w pt10">
  <div class="lzo">
    <div class="lzt">
      <h2 class="left f16 pl10"><a href="{$cat.url}" target="_blank">{$cat.name}</a></h2>
      <a class="right gray pr10" href="{$cat.url}" target="_blank">查看更多>></a>
    </div>
    <div class="left lzr over">
      <div class="w10 left">
        <!--{foreach from=$cat.goods_detail item=goods name=goods_detail}-->
        <dl>
          <dt><a href="{$goods.url}" target="_blank" title="{$goods.name|escape:html}"><img class="thm" width="220" height="220" src2="{$goods.thumb}"></a></dt>
          <dd class="thumb">
            <!--{foreach from=$goods.thumb_url item=url name=urls}-->
            <img width="30" height="30" src2="{$url.thumb_url}" data-s="{$url.img_url}">
            <!--{/foreach}-->
          </dd>
          <dd class="g"><b class="xj"><!-- {if $goods.promote_price neq "¥0.00元"} -->{$goods.promote_price}<!-- {else}-->{$goods.shop_price}<!--{/if}--></b><span class="yj">{$goods.market_price}</span></dd>
          <dd class="ms fr"><a title="{$goods.name|escape:html}" class="c3 fr" href="{$goods.url}" target="_blank">{$goods.short_name|escape:html}</a>
            <p title="{$goods.brief|escape:html}" class="gray">{$goods.brief|escape:html}</p>
          </dd>
          <dd class="sa"><span>销售:<em>{$goods.sales_volume_base|escape:html}</em></span><!--<span><a href="#" target="_blank">评论:<b>23</b></a></span><a onclick="adCar(this,5503)" title="加入购物车" class="lcar" href="javascript:;"></a>--></dd>
        </dl>
        <!--{/foreach}-->
      </div>
    </div>
    <div class="clear"></div>
    <H3 class="gray fN fr">{$cat.cat_desc}</H3>
  </div>
  <p class=lh16></p>
</div>
<!--{/foreach}-->
<!--循环二级分类-->

如图所示:

ok!完成,顺带说一下,这个循环分类以及显示分类下商品的功能之前博主我在网上找了很久都没有找到,遂,索性自己开发。最关键的是category.php里面的那个foreach循环,有兴趣的同学可以研究看看。特此分享给给位博友。

若要转载请注明出处:http://blog.csdn.net/glatchen/article/details/42238499

时间: 2024-11-08 01:15:07

ecshop商品列表页,循环显示当前分类的二级分类以及分类下的商品的相关文章

ecshop模板将商品列表页属性筛选区的品牌以LOGO形式显示

ecshop模板将商品列表页属性筛选区的品牌以LOGO形式显示 商品列表页属性筛选区品牌以LOGO形式显示1.修改 category.php 文件将(大概215行) $sql = "SELECT b.brand_id, b.brand_name, COUNT(*) AS goods_num ". 修改为 $sql = "SELECT b.brand_id,b.brand_logo, b.brand_name, COUNT(*) AS goods_num ". 把商品

让ECSHOP商品列表页和商品详细页分类树跟首页一样

如何让商品列表页或商品详情页的分类树都跟首页一样,也是显示全部所有的分类呢?修改方法:1.商品列表页修改方法:打开category.php 文件将$smarty->assign('categories',       get_categories_tree($cat_id));修改为$smarty->assign('categories',       get_categories_tree());2.商品详情页修改方法:将$smarty->assign('categories',   

ecshop实现商品列表页属性筛选区品牌筛选以LOGO形式展示

很多时候ecshop分类页的品牌都是以名称的形式显示的,下面ECSHOP开发中心的技术和大家说说ecshop 如何实现商品列表页属性筛选区品牌筛选以LOGO形式展示 1.修改 category.php 文件,将(大概220行) 找到 1 $sql = "SELECT b.brand_id, b.brand_name, COUNT(*) AS goods_num ". 替换成 1 $sql = "SELECT b.brand_id,b.brand_logo, b.brand_n

(生鲜项目)08. ModelSerializer 实现商品列表页, 使用Mixin来实现返回, 以及更加方便的ListAPIView, 以及分页的设置

第一步: 学会使用ModelSerializer, 并且会使用ModelSerializer相互嵌套功能 1. goods.serializers.py from rest_framework import serializers from goods.models import Goods, GoodsCategory # 让goods的category字段全量显示 class CategorySerializer(serializers.ModelSerializer): class Met

(生鲜项目)07. api view实现商品列表页

第一步: 环境配置 1. DRF官网: https://www.django-rest-framework.org/ 仔细查看自己当前的python版本以及django版本是否支持DRF, 然后就看看哪些支持的模块还没有下载, 由于之前安装过xadmin了, 所以这里正常情况下还有coreapi, pygments, django-guardian没有安装, 在虚拟环境中去安装就行了 注: 如果在安装coreapi的时候报错, 并且提示utf-8 decode错误, 那么就去修改pip的编码格式

在ECSHOP商品列表页和搜索页面,最后出现空商品的解决办法

有客户购买了我们的ecshop模板后,自己做了大量的修改和改动,后来出现了商品列表页面和搜索页面出现了多一个商品的问题.没有商品数据,但是多显示了一个商品的样式.下面就由我们68ecshop为大家解决一下这个问题吧!解决的方法如下:<!– {if $goods.goods_id} –> <!– {/if} –>代码框起来即可解决.

夺命雷公狗ThinkPHP项目之----企业网站26之网站前台列表页的显示和完成分页功能

我们用大I接收到我们get过来的栏目页的id然后通过文章的ar_cateid 来判断是不是属于该栏目下的,如果文章表ar_cateid = 栏目表的cate_id 那么就可以选出我们要查找的信息, 然后再遍历到视图即可... <?php namespace Home\Controller; use Think\Controller; class ListsController extends CommonController { public function lists(){ //获取栏目的i

Ecshop文章列表页显示内容摘要

本教程中讲到的“内容摘要”指的是文章内容的前 60个字符(当然也可以是前40个,前50个等等) 下面以 2.7.2版 + 官方默认模板 为例进行讲解: 1).修改 includes/lib_article.php  文件搜索 $sql = 'SELECT article_id, title, author, add_time, file_url, open_type' . 一共能搜到两处,每一处都改为 $sql = 'SELECT article_id, title,content, autho

商品列表页一次添加多个规格

可能题目的表述不是特别清晰,具体有一下截图看这会比较明显 页面上的功能描述1. 当页面加载完成后,需要根据不同规格的商品刷新出对应规格商品的价格,2.重置默认数量,这个功能在input标签中设置默认的value值即可,或者添加一个js函数,重置对应class集合中的值,这个不多说,比较简单,3.点击添加购物车,需要把例如上图中的不同规格中的商品能全部添加到购物车 解题思路,要求1,这个任务比较简单,由于这种方式,后台里的商品规格选项必须用单选按钮来做,否则页面效果不容易实现具体要实现什么样式自己