2013-06-03 61 views
2

大家好我試圖LIS subcats的主要類別是這樣的:如何列出子類別?

Maincat1
-subcat
Maincat2
-subcat

請參閱 image

這是上市的主要貓代碼:

在global_header .php

$sql_select_cats_list = $db->query("SELECT category_id, items_counter, hover_title, image_path FROM 
      " . DB_PREFIX . "categories WHERE parent_id=0 AND hidden=0 AND user_id=0 AND enable_auctions=1 ORDER BY order_id ASC, name ASC"); 

    $template->set('sql_select_cats_list', $sql_select_cats_list); 

    $category_box_content = $template->process('categories_box.tpl.php'); 
    $template->set('category_box_content', $category_box_content); 

這是categories_box.tpl.php

<? 
while ($cats_header_details = $db->fetch_array($sql_select_cats_list)) 
{ 
    $category_link = process_link('categories', 
     array('category' => $category_lang[$cats_header_details['category_id']], 
       'parent_id' => $cats_header_details['category_id'])); 
?> 
    <li> 
     <a style="font-size: 11px; font-weight: normal; text-decoration:none;" 
      href="<?=$category_link;?>" 
      <?=((!empty($cats_header_details['hover_title'])) ? 'title="' . $cats_header_details['hover_title'] . '"' : '');?> 
      alt=""> 
       <img src="<?=$cats_header_details['image_path'];?>" width="10" height="8" border="0" /> 
       &nbsp; 
       <?=$category_lang[$cats_header_details['category_id']];?> 
       <?=(($setts['enable_cat_counters']) ? 
        (($cats_header_details['items_counter']) ? 
         '(<strong>' . $cats_header_details['items_counter'] . '</strong>)' : 
         '') : 
        ''); 
       ?> 
     </a> 
    </li> 
<? 
} 
?> 

這是我原來的功能上 function.php上市類別

function category_navigator ($parent_id, $show_links = true, $show_category = true, $page_link = null, $additional_vars = null, $none_msg = null, $reverse_categories = false) 
{ 
    global $reverse_categoy_lang, $category_lang, $db; 

    (string) $display_output = NULL; 
    (int) $counter = 0; 

    $none_msg = ($none_msg) ? $none_msg : GMSG_ALL_CATEGORIES; 

    $page_link = ($page_link) ? $page_link : $_SERVER['PHP_SELF']; 
    if($parent_id > 0) 
    { 
     $root_id = $parent_id; 
     while ($root_id > 0) 
     { 
      $row_category = $db->get_sql_row("SELECT category_id, name, parent_id FROM 
       " . DB_PREFIX . (($reverse_categories) ? 'reverse_categories' : 'categories') . " WHERE 
       category_id=" . $root_id . " LIMIT 0,1"); 

      if($counter == 0) 
      { 
       $display_output = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']]; 
       $display_output = (!empty($display_output)) ? $display_output : $row_category['name']; 
      } 
      else if($parent_id != $root_id) 
      { 
       $category_name = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']]; 
       $category_name = (!empty($category_name)) ? $category_name : $row_category['name']; 

       $display_output = (($show_links) ? '<a href="' . $page_link . '?parent_id=' . $row_category['category_id'] . '&name=' . sanitize_var($category_name) . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '') . '">' : '') . $category_name . (($show_links) ? '</a>' : '') . ' > ' . $display_output; 
      } 
      $counter++; 
      $root_id = $row_category['parent_id']; 
     } 
     $display_output = (($show_links && $show_category) ? '<a href="' . $page_link . '?' . $additional_vars . '"><b> ' . GMSG_CATEGORY . ':</b></a> ' : '') . $display_output; 
    } 

    $display_output = (empty($display_output)) ? $none_msg : $display_output; 

    return $display_output; 
} 

數據庫名稱是:

table : categories 
    | category_id + parent_id +  name   | 
    | 1    | 0  |  Flower   | 
    | 2    | 0  |  Wall Decor  | 
    | 3    | 0  |  Stylish Living | 
    | 4    | 1  |  Mug   | 
    | 5    | 1  |  Sun Flower  | 


我需要幫助來做到這一點。
導航菜單

<div id="navigation"> 
     <ul> <li><a href="#">News</a> 
       <ul> <li>National News</li> <li>International News</li> <li>Sports News </li> <li>Hollywood news</li> </ul> 
      </li> 
      <li> 
      <a href="#">Technology</a> 
       <ul> <li>IT/Software </li> <li>Hardware</li> <li>Iphone</li> <li>Neuro-Science</li> </ul> 
      </li> 
      <li> 
      <a href="#">Sports</a> 
       <ul> <li>Cricket</li> <li>Tenis</li> <li>Badminton</li> <li>Hockey</li> </ul> 
      </li> 
      <li> 
      <a href="#">Contry</a> 
       <ul> <li>India</li> <li>Shree lanka </li> <li>Bangaladesh</li> <li>England</li> </ul> 
      </li> 
     </ul> 
    </div> 

最後修改:

解決特別感謝@furas

代碼:

<div id="navigation"> 
      <ul> <? 
     foreach($menu_multiarray as $main_category) 
     { 
     $cats_link = process_link('categories', array('category' => $main_category['category']['name'], 'parent_id' => $main_category['category']['category_id'])); 
    ?> 
    <li> 
    <a style="font-size: 11px; font-weight: normal; text-decoration:none;" href="<?=$cats_link;?>" <?=((!empty($main_category['hover_title'])) ? 'title="' . $main_category['hover_title'] . '"' : '');?> alt=""> 
    <img src="<?=$main_category['image_path'];?>" width="10" height="8" border="0" />&nbsp;<?=$main_category['category']['name'];?> 
      <?=(($setts['enable_cat_counters']) ? (($main_category['items_counter']) ? '(<strong>' . $main_category['items_counter'] . '</strong>)' : '') : '');?></a> 
     <? 
     foreach($main_category["subcategories"] as $sub_category) 
     { 
     $sub_cats_link = process_link('categories', array('category' => $sub_category['name'], 'parent_id' => $sub_category['category_id'])); 
     ?>  
    <ul> 
     <li> 
     <a style="font-size: 11px; font-weight: normal; text-decoration:none;" href="<?=$sub_cats_link;?>" <?=((!empty($sub_category['hover_title'])) ? 'title="' . $sub_category['hover_title'] . '"' : '');?> alt=""> 
     <img src="<?=$sub_category['image_path'];?>" width="10" height="8" border="0" />&nbsp;<?=$sub_category['name'];?> 
      <?=(($setts['enable_cat_counters']) ? (($sub_category['items_counter']) ? '(<strong>' . $sub_category['items_counter'] . '</strong>)' : '') : '');?></a> 
     </li> 
     </ul> 
     <? } ?> 
     </li> 
     <? } ?> 
    </ul> 
     </div> 

回答

0

僞代碼:

get_from_database_main_categories(); 

while(i_have_categories) { 
    print_category(); 
    get_from_database_subcategories_for_current_category(); 
    print_subcategories(); 
} 

也許喲ü可以得到一個SQL查詢中的所有類別和子類別,並創建類似的方式菜單How do I create a nested list inside of a while loop?

編輯:

我不給你完整的代碼,但大部分。

因爲你的代碼在兩個文件我拆我的代碼分爲兩個部分 - 獲取數據和打印數據

global_header.php(獲取數據)

function get_categories_by_parent($parent_id) 
{ 
    global $db; 

    return $db->query(
       "SELECT category_id, items_counter, hover_title, image_path ". 
       "FROM ".DB_PREFIX."categories ". 
       "WHERE parent_id=".$parent_id." AND hidden=0 AND user_id=0 AND enable_auctions=1 ". 
       "ORDER BY order_id ASC, name ASC" 
      ); 
} 

$categories = get_categories_by_parent(0); 

$menu_multiarray = array(); 
$i = 0; 

while ($main_category = $db->fetch_array($categories)) 
{ 
    $menu_multiarray[$i] = array(); 
    $menu_multiarray[$i]['category'] = $main_category; 
    $menu_multiarray[$i]['subcategories'] = array(); 

    $subcategories = get_categories_by_parent($main_category['category_id']); 

    while ($sub_category = $db->fetch_array($subcategories)) 
    { 
     $menu_multiarray[$i]['subcategories'][] = $sub_category; 
    } 

    $i++; 
} 

$template->set('menu_multiarray', $menu_multiarray); 

categories_box.tpl.php(打印數據)

foreach($menu_multiarray as $main_category) 
{ 
    // print main category using 
    // $main_category['category']['category_id'] 
    // $main_category['category']['items_counter'] 
    // $main_category['category']['hover_title'] 
    // $main_category['category']['image_path'] 

    $limit = 10; 

    foreach($main_category['subcategories'] as $sub_category) 
    { 
     // print sub category using 
     // $sub_category['category_id'] 
     // $sub_category['items_counter'] 
     // $sub_category['hover_title'] 
     // $sub_category['image_path'] 

     $limit--; 
     if($limit == 0) break; 
    } 

    if($limit > 0) echo "link to rest of subcategorie"; 
} 

當然,我沒有測試代碼,但它告訴你要走哪條路。

+0

這麼友善,我不擅長php,如果你能給出完整的代碼來幫助我,我會感激你的。 –

+0

謝謝furas,你不是人類:)天使:)再次感謝只有一個問題geting這個錯誤?致命錯誤:在第93行的C:\ AppServ \ www \ wor \ global_header.php中的非對象上調用成員函數query()LINE 93 IS:return $ db-> query(返回空爲什麼。 –

+0

我看到。我在'get_categories_by_parent'函數中使用'$ db',但是'$ db'聲明在外,你必須在該函數的第一行添加'global $ db;'(就像你的函數'category_navigator')。 – furas

1

你可以做到這一點,而無需多次查詢。

SELECT * FROM categories ORDER BY parent ASC, order_id ASC, name ASC 

僞代碼:

while ($result = fetch_array()) { 
    if ($result['parent'] == 0) { 
    $categories[$result['category_id']] = $result; 
    } 
    else { 
    $categories[$result['parent']]['categories'][$result['category_id']] = $result; 
    } 
} 

這將父0所有類別直接添加到陣列,所有子類別將被添加到父內部的子陣列。家長訂購將確保父類別在需要時已經存在。

這隻會在一個級別上工作,對於更多級別,您需要查看數組中的所有現有類別以找到正確的父級。但我認爲它仍然會比多個SQL查詢更好。

+0

我怎樣才能設置限制10的子類別,和其餘顯示所有SUBCATEGORIES LINK –

+0

帶有一個查詢的版本要好得多。我只提到過這種可能性,但沒有想到如何做到這一點。 – furas

相關問題