2012-09-24 80 views
0

我正在使用Zen Cart,目前正在爲電子商務網站構建菜單。我已經設法弄清楚如何調用所有類別,但客戶只需要顯示前四個類別。目前有超過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> 

我希望這是一個簡單的解決方案!

謝謝!

+0

什麼乾脆直接使用'LIMIT'子句中的SQL? – Lion

+0

這是QUERY我不確定哪裏會去 $ categories_query =「select c.categories_id,cd.categories_name,c.parent_id from」。 TABLE_CATEGORIES。 「 C, 」 。 TABLE_CATEGORIES_DESCRIPTION。 「cd 其中c.categories_id = cd.categories_id 和c.categories_status = 1」。 「和cd.language_id ='」。 (int)$ _ SESSION ['languages_id']。 「' order by c.parent_id,c.sort_order,cd.categories_name」; $ categories = $ db-> Execute($ categories_query); –

+0

如果您只需要結果集中的前四行查詢返回,則可以在'order by'子句後面(如果使用的是MySql)在SQL查詢中追加'limit 4'。 – Lion

回答

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); 

我不知道你的數據庫的結構,但你可以使用上面做了2步方法。第一個是獲得前四個類別。第二個查詢是獲得這4個類別的子類別。

我不太贊成子查詢 - 我儘可能避免它們,所以在我看來,這不是一個非常有效的解決方案。你可以有效地從那裏穿過第一個查詢並獲得IDS像這樣:

// 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

+0

我這樣做了,但是它並沒有把這些分類稱爲前四類的子類別。有任何想法嗎? –

+0

添加了回覆。 –

相關問題