2

大家好,我正在尝试在以下主要类别中列出子猫:

Maincat1
-subcat
Maincat2
-subcat

请看 图片

这是列出主要猫的代码:

在 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>
4

2 回答 2

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 查询更好。

于 2013-06-04T11:27:46.250 回答
0

伪代码:

get_from_database_main_categories();

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

或者,也许您可​​以在一个 SQL 查询中获取所有类别和子类别,并以类似于如何在 while 循环中创建嵌套列表的方式创建菜单?

编辑:

我没有给你完整的代码,但大部分都是。

因为您在两个文件中有代码,所以我将代码分成两部分 - 获取数据和打印数据

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";
}

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

于 2013-06-03T02:14:02.667 回答