0

我正在使用 Zen Cart,目前正在为电子商务网站构建菜单。我已经设法弄清楚如何调用所有类别,但客户只希望显示前 4 个类别。现在有 20 多个类别,我只需要前 4 个即可显示在列表中。我不确定做到这一点的最佳和最简单的方法。

有没有办法调用列表

例子:

<ul>
   <li>Menu One</li>
   <li>Menu Two
      <ul>
         <li>Submenu One</li>
         <li>Submenu Two</li>
         <li>Submenu Three</li>
      </ul>
   </li>
   <li>Menu Three</li>
   <li>Menu Four</li>
   <li>Menu Five</li>
   <li>ect...</li>
</ul>

然后只显示

<ul>
   <li>Menu One</li>
   <li>Menu Two
      <ul>
         <li>Submenu One</li>
         <li>Submenu Two</li>
         <li>Submenu Three</li>
      </ul>
   </li>
   <li>Menu Three</li>
   <li>Menu Four</li>
</ul>

我希望这是一个简单的解决方案!

谢谢!

4

1 回答 1

0

根据您的回复,您的查询应该是:

// Categories
$lid = (int) $_SESSION['languages_id']; 
$sql = "SELECT c.categories_id, cd.categories_name, c.parent_id "
     . "FROM " . TABLE_CATEGORIES . " c, " 
     . TABLE_CATEGORIES_DESCRIPTION . " cd "
     . "WHERE c.categories_id = cd.categories_id and c.categories_status = 1 " 
     . "AND cd.language_id = '{$lid}' "
     . "ORDER BY c.parent_id, c.sort_order, cd.categories_name "
     . "LIMIT 4"; 
$categories = $db->Execute($sql);

// Subcategories of the above
$lid = (int) $_SESSION['languages_id']; 
$sql = "SELECT c.categories_id, cd.categories_name, c.parent_id "
     . "FROM " . TABLE_CATEGORIES . " c, " 
     . TABLE_CATEGORIES_DESCRIPTION . " cd "
     . "WHERE c.categories_id IN ("
     .     "SELECT c.categories_id "
     .     "FROM " . TABLE_CATEGORIES . " c, " 
     .     TABLE_CATEGORIES_DESCRIPTION . " cd "
     .     "WHERE c.categories_id = cd.categories_id and c.categories_status = 1 " 
     .     "AND cd.language_id = '{$lid}' "
     .     "ORDER BY c.parent_id, c.sort_order, cd.categories_name "
     .     "LIMIT 4" 
     . "); 
$subcategories = $db->Execute($sql);

我不知道你的数据库的结构,但你可以使用上面的方法来做一个两步的方法。第一个是获得前 4 个类别。第二个查询是获取这 4 个类别的子类别。

我不太赞成子查询——我尽可能避免使用它们,所以在我看来这不是一个非常有效的解决方案。您可以有效地遍历第一个查询并从那里获取 id,如下所示:

// Categories
$lid = (int) $_SESSION['languages_id']; 
$sql = "SELECT c.categories_id, cd.categories_name, c.parent_id "
     . "FROM " . TABLE_CATEGORIES . " c, " 
     . TABLE_CATEGORIES_DESCRIPTION . " cd "
     . "WHERE c.categories_id = cd.categories_id and c.categories_status = 1 " 
     . "AND cd.language_id = '{$lid}' "
     . "ORDER BY c.parent_id, c.sort_order, cd.categories_name "
     . "LIMIT 4"; 
$categories = $db->Execute($sql);

// Loop through the $categories and get the ids
$category_ids = array();
foreach ($categories as $category)
{
    $category_ids[] = $caregory->categories_id;
}
// Subcategories of the above
$lid = (int) $_SESSION['languages_id']; 
$sql = "SELECT c.categories_id, cd.categories_name, c.parent_id "
     . "FROM " . TABLE_CATEGORIES . " c, " 
     . TABLE_CATEGORIES_DESCRIPTION . " cd "
     . "WHERE c.categories_id IN (" . implode(",", $category_ids) . ")";
$subcategories = $db->Execute($sql);

在我看来,上述内容比子查询要好得多。

高温高压

狮子的学分

于 2012-09-24T21:04:52.653 回答