ecshop2.6.2奇数偶数分类树处理

2010-01-29 17:11 来源:www.chinab4c.com 作者:admin

1:ecshop分类树只取得某一级分类下所有分类
function get_categories_tree1($cat_id = 0)
{
    if ($cat_id > 0)
    {
        $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
        $parent_id = $GLOBALS['db']->getOne($sql);
    }
    else
    {
        $parent_id = 0;
    }

    /*
     判断当前分类中全是是否是底级分类,
     如果是取出底级分类上级分类,
     如果不是取当前分类及其下的子分类
    */
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id' AND is_show = 1 ";
    if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
    {
        /* 获取当前分类及其子分类 */
        $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, a.is_show,' .
                    'b.cat_id AS child_id, b.cat_name AS child_name, 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 AND b.is_show = 1 ' .
                "WHERE a.parent_id = '$parent_id' and a.cat_id = '$cat_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC";
    }
    else
    {
        /* 获取当前分类及其父分类 */
        $sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order, b.is_show ' .
                'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
                'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
                "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC";
    }
    $res = $GLOBALS['db']->getAll($sql);

    $cat_arr = array();
    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']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

            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']);
            }
        }
    }

    return $cat_arr;
}


2:index.php
$ar = get_categories_tree1(10);//单独取得每个一级分类的所有子分类
if($ar){
 $c = count($ar[10][children]);//计算子分类个数
 $smarty -> assign('prev',$c-1);//设置子分类个数的最大下标
 //$smarty-> assign('front',array_slice($ar,0,-1));//
 $smarty -> assign('gc',count($ar[10][children])%2);//判断是奇数还是偶数
 $smarty -> assign('tree',$ar);//分类树数组

}

3:
{if $tree}
    <!--------------如果是奇数做奇数循环处理开始---------->
   {if $gc == 1}
         {foreach from = $tree item = item}
     <dl class="d2">
     <h3>{$item.name}</h3>
     {foreach from = $item.children item = item1 name = name}
         <!--------------循环基数下面的除最后一个分类下的所有分类开始---------->
         {if $smarty.foreach.name.index<$prev}
       {if $smarty.foreach.name.index %2 == 0}
       <dd>
       {/if}
         <a href="{$item1.url}">{$item1.name}</a>{if $smarty.foreach.name.index %2 == 0}|{/if}
       {if $smarty.foreach.name.index %2 == 1}
       </dd>
       {/if}
      <!--------------循环基数下面的除最后一个分类下的所有分类结束---------->
      {else}
       <!--------------循环基数下面的除最后一个分类---------->
           <dd> <a href="{$item1.url}">{$item1.name}</a></dd>
      {/if}
     {/foreach}
     </dl>
    {/foreach}
    <!--------------如果是奇数做奇数循环处理结束---------->
   {else}
   <!--------------如果是偶数做奇数循环处理开始---------->
      {foreach from = $tree item = item}
     <dl class="d2">
     <h3>{$item.name}</h3>
     {foreach from = $item.children item = item1 name = name}
      {if $smarty.foreach.name.index %2 == 0}
      <dd>
      {/if}
        <a href="{$item1.url}">{$item1.name}</a>{if $smarty.foreach.name.index %2 == 0}|{/if}
      {if $smarty.foreach.name.index %2 == 1}
      </dd>
      {/if}
     {/foreach}
     </dl>
    {/foreach}
   {/if}
 <!--------------如果是偶数做奇数循环处理结束---------->
  {/if}

   来源:中国B4C电子商务