index.dwt里代码  

            <!--{foreach from=get_categories_tree(0) item=cat name=cat0}-->
               <div id="JS_hide_side_menu_{$smarty.foreach.cat0.iteration}" class="item all" οnmοuseοver="showMenu(this,'side','{$smarty.foreach.cat0.iteration }')" οnmοuseοut="hideMenu(this,'side','{$smarty.foreach.cat0.iteration}')">
                 <div class="hideMenu {if $smarty.foreach.cat0.iteration >3}to{$smarty.foreach.cat0.iteration}{/if}">
                   <div class="menuList">
                       <div>
<h4>热门品牌</h4>
                       <p>
                       <!--{foreach from=get_brands1($GLOBALS['smarty']->_var['cat']['id']) item=bchilder name=name1}-->
                       {if $smarty.foreach.name1.iteration < 4} <a  href="{$bchilder.url}" target="_blank" title=""><img src="data/brandlogo/{$bchilder.brand_logo}"  width="91" height="35" alt="" /></a> {/if}
                       <!--{/foreach}-->
                       </p>
                    </div>
                     <!--{foreach from=$cat.cat_id item=child name=name}-->
                     <!--{if $smarty.foreach.name.iteration lt 7}-->
                     <div class="list {if $smarty.foreach.name.first}first{/if} clearfix">
                       <h4>{$child.name|escape:html}</h4>
                       <p><!--{foreach from=$child.cat_id item=childer}-->
                         <a href="{$childer.url}" target="_blank" title="{$childer.name|escape:html}">&nbsp;{$childer.name|escape:html}&nbsp;</a>|<!--{/foreach}--></p>
                     </div>
                     <!--{/if}-->
                     <!--{/foreach}-->
                   </div>
                 </div>
                 <dl>
                   <dt> <a href="{$cat.url}" target="_blank" title="{$cat.name|escape:html}">{$cat.name|escape:html}</a> </dt>
                 </dl>
               </div>
               <!--{/foreach}-->

index.php代码

//得到分类下的品牌
function get_brands1($url = 0, $app = 'brand')
{
    preg_match("/\d+/i",$url,$matches);
    $cat = $matches[0];
    $children = ($cat > 0) ? ' AND ' . get_children($cat) : '';
    $sql = "SELECT b.brand_id, b.brand_name, b.brand_logo, b.brand_desc, COUNT(*) AS goods_num, IF(b.brand_logo > '', '1', '0') AS tag ".
            "FROM " . $GLOBALS['ecs']->table('brand') . "AS b, ".
                $GLOBALS['ecs']->table('goods') . " AS g ".
            "WHERE g.brand_id = b.brand_id $children AND is_show = 1 " .
            " AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
            "GROUP BY b.brand_id HAVING goods_num > 0 ORDER BY tag DESC, b.sort_order ASC";
    $row = $GLOBALS['db']->getAll($sql);
    foreach ($row AS $key => $val)
    {
        $row[$key]['url'] = build_uri($app, array('cid' => $cat, 'bid' => $val['brand_id']), $val['brand_name']);
        $row[$key]['brand_desc'] = htmlspecialchars($val['brand_desc'],ENT_QUOTES);
    }
    return $row;
}
//*** 调用商品分类指定分类下级分类
function get_parent_id_tree($parent_id)
{
    $three_c_arr = array();
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
    if ($GLOBALS['db']->getOne($sql))
    {
        $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
                'FROM ' . $GLOBALS['ecs']->table('category') .
                "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
        $res = $GLOBALS['db']->getAll($child_sql);
        foreach ($res AS $row)
        {
            if ($row['is_show'])
               $three_c_arr[$row['cat_id']]['id']   = $row['cat_id'];
               $three_c_arr[$row['cat_id']]['name'] = $row['cat_name'];
               $three_c_arr[$row['cat_id']]['url'] = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
        }
    }
    return $three_c_arr;
}

这里有个写法 foreach from里面可以写函数 函数里还可以写变量。。。。