既然网上找不到好办法,那就是不要循环了,我们调用指定栏目,把需要调用的大栏目一一写出来,虽然麻烦,但是还算是使用。
网上能搜到很多调用指定栏目的方法,一下方法是我总结的 ,亲测有效:
第一步:打开include下的lib_insert.php,这是一个封装函数的文件,在里边添加两个自定义函数:
(1)循环调用栏目和子栏目的函数(里边镶嵌有自己的css,需要手动修改)
function insert_get_catlist($arr){
$sql = 'SELECT cat_id,cat_name ,parent_id,is_show,sort_order ' .
'FROM ' . $GLOBALS['ecs']->table('category') .
"WHERE parent_id = ".$arr['id']." AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC limit 7";
$res = $GLOBALS['db']->getAll($sql);
$cinfo = $GLOBALS['db']->getRow("SELECT * FROM ". $GLOBALS['ecs']->table('category') ." where cat_id = ".$arr['id']."");
$x = 1;
$cinfo['url'] = build_uri('category', array('cid' => $cinfo['cat_id']), $cinfo['cat_name']);
//$str='<div class="floor_title"><H2>'.$x.'F</H2><p><a href="'.$cinfo['url'].'" target="_blank">'.$cinfo['cat_name'].'</a></p></div>';
$str.='<div class="floor_menu"><ul>';
foreach ($res AS $row){
if ($row['is_show'])
{
$cat_arr[$row['cat_id']]['id'] = $row['cat_id'];
$cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
$cat_arr[$row['cat_id']]['sort_order'] = $row['sort_order'];
$cat_arr[$row['cat_id']]['url'] = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
$str.='<li><a href="'.$cat_arr[$row['cat_id']]['url'].'">'.$cat_arr[$row['cat_id']]['name'].'</a></li>';
}
$x++;
}
$str.='</ul></div>';
return $str;
}
function insert_get_goodspl($arr){
return $GLOBALS['db']->getOne("select count(*) from ". $GLOBALS['ecs']->table('comment') . " where id_value = ".$arr['id']);
}
function insert_get_goodsxs($arr){
return $GLOBALS['db']->getOne("select count(*) from ". $GLOBALS['ecs']->table('order_goods') . " where goods_id = ".$arr['id']);
}
(2)调用该分类下的产品函数
function insert_get_category_goodslist($arr){
$num = $arr['num'];
$sql ='SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
"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, b.brand_name ' .
'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_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 ';
$children = get_children($arr['id']);
$sql.= " and g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
"g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';
$sql .=' ORDER BY g.sort_order, g.last_update DESC';
$res = $GLOBALS['db']->selectLimit($sql, $num);
$idx = 0;
$goods = 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']);
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
} else{
$goods[$idx]['promote_price'] = '';
}
$goods[$idx]['id'] = $row['goods_id'];
$goods[$idx]['name'] = $row['goods_name'];
$goods[$idx]['brief'] = $row['goods_brief'];
$goods[$idx]['brand_name'] = $row['brand_name'];
$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]['market_price'] = price_format($row['market_price']);
$goods[$idx]['shop_price'] = price_format($row['shop_price']);
$goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$goods[$idx]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$goods[$idx]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
$goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'], $row['goods_name_style']);
$str.='<li>
<span><a href="'.$goods[$idx]['url'].'" /><img src="'. $goods[$idx]['thumb'].'" width=150 height=150></a></span>
<p><a href="'.$goods[$idx]['url'].'" />'.$goods[$idx]['short_style_name'].'</s></p>
<dt><i class="price">'.$goods[$idx]['shop_price'].'</i><i class="yuanjia">'.$goods[$idx]['market_price'].'</i></dt>
<div class="goumai2"><a href="javascript:addToCart('.$goods[$idx]['id'].')">加入购物车</a></div>
</li>';
$idx++;
}
return $str;
}
function insert_get_cart_list(){
include_once('includes/lib_order.php');
$cart_goods = get_cart_goods();
$str.='<p class="good_cart">'.$cart_goods['total']['real_goods_count'].'</p>
<span class="fixeBoxSpan"></span> <strong>购物车</strong>
<div class="cartBox">
<div class="bjfff"></div>';
if($cart_goods['goods_list']){
$str.='<div class="cartBoxC "><ul>';
foreach($cart_goods['goods_list'] as $key=>$value){
$value['url'] = build_uri('goods', array('gid' => $value['goods_id']), $value['goods_name']);
$str.='<li>
<div class="p-img"> <a target="_blank" href="'.$value['url'].'"> <img src="'.$value['goods_thumb'].'"> </a> </div>
<div class="p-name"> <a target="_blank" href="'.$value['url'].'">'.$value['goods_name'].'</a> </div>
<div class="p-detail"> <span class="p-price"> <strong>¥'.$value['goods_price'].'</strong> × '.$value['goods_number'].' </span> </div>
</li>';
}
$str.='</ul></div>';
$str.='<div class="cartBoxFoot"><span>小计(不含运费):</span> <em>¥</em> <strong>'.$cart_goods['total']['goods_price'].'</strong>
<a id="btn-payforgoods" href="flow.php">去购物车结算</a> </div></div>';
}else{
$str.='<div class="message">购物车内暂无商品,赶紧选购吧</div>';
}
return $str;
}
封装好之后 我们就可以直接调用了
调用栏目就是:{insert name='get_catlist' id=4}
调用产品就是:{insert name='get_category_goodslist' id=4 num=8}
文章点评