如何列出子类别?

时间:2013-06-03 01:53:59

标签: php list categories

大家好我试图在主要类别中使用这样的子类:

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>

2 个答案:

答案 0 :(得分: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查询更好。

答案 1 :(得分: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";
}

当然我没有测试代码,但它告诉你要走哪条路。