仅显示列表中的第一个结果

时间:2012-09-24 20:00:04

标签: php menu hide

我正在使用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>

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

谢谢!

1 个答案:

答案 0 :(得分: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);

我认为上面的内容远比子查询好。

HTH

Lion

的信用