在XCart中,如何列出类别中的子类别?

时间:2011-08-15 03:21:40

标签: php smarty x-cart

在我的XCart 4.4.2安装中,我有几个主要类别的产品,每个产品包含几个子类别。在主页上,我想列出每个类别中的子类别,但是无法从此代码中访问welcome.tpl中的子类别:

{foreach from=$categories_menu_list item=c name=categories}
  <a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}">
    <li>
        <img src="{$c.image_path|amp}" alt="{$c.category|escape}"/>
        <strong>{$c.category}</strong><br/>

        <!-- list subcategories here-->
        {php}
          $parentid = $c.categoryid;

          $categoryNames = func_query_column("SELECT category FROM $sql_tbl[categories] WHERE parentid = " . $parentid);
          print_r($categoryNames);
        {/php}
    </li>
  </a>
{/foreach}

有人可以帮我生成生成子类别列表所需的PHP / SMARTY代码吗?谢谢!

2 个答案:

答案 0 :(得分:2)

最好在php脚本中定义一个子类别数组,而不是在模板中定义。我可以为您提供以下解决方案:

修补包含include / common.php

@@ -90,6 +90,14 @@
         // Get categories menu data
         if (!empty($categories)) {
             $smarty->assign('categories_menu_list', $categories);
+
+            if (!isset($cat) || 0 == intval($cat)) {
+                $extended_categories = func_get_categories_list(0, true, true, 1);
+
+                if (!empty($extended_categories)) {
+                    $smarty->assign('extended_categories_list', $extended_categories);
+                }
+            }
         }

         if ($active_modules['Manufacturers']) {

(标有+的行应添加到代码中)

皮肤/你的皮肤/客户/主/ welcome.tpl

{foreach from=$categories_menu_list item=c}
<a href="home.php?cat={$c.categoryid}" title="{$c.category|escape}">
  <li>
  <img src="{$c.image_path|amp}" alt="{$c.category|escape}"/>
          <strong>{$c.category}</strong><br/>
    <!-- list subcategories here-->
    {foreach from=$extended_categories_list item=ec}
      {if $ec.parentid eq $c.categoryid}{$ec.category|escape}<br />{/if}
    {/foreach}
  </li>
</a>
{/foreach}

答案 1 :(得分:1)

经典答案对我来说并不适用。这是我使用的:

    // Get categories menu data
    if (!empty($categories)) {
        $smarty->assign('categories_menu_list', $categories);

        foreach($categories as $c){
            $ext_cats = func_get_categories_list($c['categoryid'], true, true, 1);
            if(!empty($ext_cats)){
                $extended_categories[$c['categoryid']] = $ext_cats;
            }
            $ext_cats = "";
        }

        if (!empty($extended_categories)) {
            $smarty->assign('extended_categories_list', $extended_categories);
        }   
    }

循环显示每个类别并获取所有子类别。