大家好我試圖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" />
<?=$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" /> <?=$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" /> <?=$sub_category['name'];?>
<?=(($setts['enable_cat_counters']) ? (($sub_category['items_counter']) ? '(<strong>' . $sub_category['items_counter'] . '</strong>)' : '') : '');?></a>
</li>
</ul>
<? } ?>
</li>
<? } ?>
</ul>
</div>
這麼友善,我不擅長php,如果你能給出完整的代碼來幫助我,我會感激你的。 –
謝謝furas,你不是人類:)天使:)再次感謝只有一個問題geting這個錯誤?致命錯誤:在第93行的C:\ AppServ \ www \ wor \ global_header.php中的非對象上調用成員函數query()LINE 93 IS:return $ db-> query(返回空爲什麼。 –
我看到。我在'get_categories_by_parent'函數中使用'$ db',但是'$ db'聲明在外,你必須在該函數的第一行添加'global $ db;'(就像你的函數'category_navigator')。 – furas