我想列出主要类别和子类别,但不知道如何?
我想做的就像这样
Main cat 1
-subcat 1
-subcat 2
-subcat others
Main cat 2
-subcat 1
-subcat 2
-subcat others
我有两个部分代码,只打印像这样的主要类别,
主要猫1 主要cat2 主要cat3
这是总代码尝试不同的方法,但不能这样做。
$sql_select_cats_list = $db->query("SELECT category_id, items_counter, hover_title 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");
$cats = array();
while($row=$db->fetch_array($sql_select_cats_list)) {
$cats[$row['category_id']] = $row;
}
$cat_ids = implode(', ', array_keys($cats));
$sql_select_subcats_list = $db->query("SELECT sub.category_id, sub.items_counter, sub.hover_title, parent.order_id FROM " . DB_PREFIX . "categories sub LEFT JOIN " . DB_PREFIX . "categories parent ON (parent.category_id=sub.parent_id) WHERE parent.category_id IN (" . $cat_ids . ") AND sub.hidden=0 AND sub.user_id=0 AND sub.enable_auctions=1 ORDER BY parent.order_id ASC, sub.order_id ASC, sub.name ASC");
while($row=$db->fetch_array($sql_select_subcats_list)) {
if(!isset($subs[$row['parent_id']])) {
$subs[$row['parent_id']] = array();
}
$subs[$row['parent_id']][] = $row;
}
$template->set('cats_list', $cats);
$template->set('subs_list', $subs);
$category_box_content = $template->process('categories_box.tpl.php');
$template->set('category_box_content', $category_box_content);
我正在尝试使用以下代码列出类别和子类别:
<?php
foreach($cats as $parent_id => $cats_header_details) {
$category_link = process_link('categories', array('category' => $category_lang[$cats_header_details['category_id']], 'parent_id' => $cats_header_details['category_id']));
// subcategory links:
if(isset($subs_list[$parent_id])) {
foreach($subs_list[$parent_id]) as $sub) {
// do something with your subcategory
}
}
?>
<li><a style="font-size: 11px; font-weight: normal;" href="<?=$category_link;?>"
<?=((!empty($cats_header_details['hover_title'])) ? 'title="' . $cats_header_details['hover_title'] . '"' : '');?> alt="">
<img src="themes/<?=$setts['default_theme'];?>/img/arrow.gif" border="0" hspace="4"><?=$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> <?
}
?>
我得到这个警告:想想foreach回来了!无法找到解决问题的方法。
Warning: Invalid argument supplied for foreach() in C:\AppServ\www\site\themes\temamiz\templates\categories_box.tpl.php on line 14
使用以下代码打印页面上的主要类别。
<?=$category_box_content;?>
答案 0 :(得分:0)
基本上更改您的模板前代码,首先从您的类别中获取所有category_ids。然后获取与该列表匹配的子类列表。
现在处理sub,命令它们可以通过父id(第二个循环)轻松找到。
然后,不是将sql资源传递给模板(错误形式),而是分发2个数组(猫和子类)。
在模板中,然后遍历类别,然后在该循环中,您有第二个循环,它仅循环该特定类别的子类(如果有的话)。
在代码中,像这样:
$sql_cats_list = $db->query("SELECT category_id, items_counter, hover_title 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");
$cats = array();
while($row=$db->fetch_array($sql_cats_list)) {
$cats[$row['category_id']] = $row;
}
$cat_ids = implode(', ', array_keys($cats));
$sql_subcats_list = $db->query("SELECT sub.category_id, sub.items_counter, sub.hover_title, parent.order_id FROM " . DB_PREFIX . "categories sub LEFT JOIN " . DB_PREFIX . "categories parent ON (parent.category_id=sub.parent_id) WHERE parent.category_id IN (" . $cat_ids . ") AND sub.hidden=0 AND sub.user_id=0 AND sub.enable_auctions=1 ORDER BY parent.order_id ASC, sub.order_id ASC, name ASC");
while($row=$db->fetch_array($sql_subcats_list)) {
if(!isset($subs[$row['parent_id']])) {
$subs[$row['parent_id']] = array();
}
$subs[$row['parent_id']][] = $row;
}
$template->set('cats_list', $cats);
$template->set('subs_list', $subs);
$category_box_content = $template->process('categories_box.tpl.php');
$template->set('category_box_content', $category_box_content);
我认为这是您的categories_box.tpl.php文件(?)
<?php
foreach($cats as $parent_id => $cats_header_details) {
$category_link = process_link('categories', array('category' => $category_lang[$cats_header_details['category_id']], 'parent_id' => $cats_header_details['category_id']));
// subcategory links:
if(isset($subs_list[$parent_id])) {
foreach($subs_list[$parent_id] as $sub) {
// do something with your subcategory
}
}
?>
<li><a style="font-size: 11px; font-weight: normal;" href="<?=$category_link;?>"
<?=((!empty($cats_header_details['hover_title'])) ? 'title="' . $cats_header_details['hover_title'] . '"' : '');?> alt=""><img src="themes/<?=$setts['default_theme'];?>/img/arrow.gif" border="0" hspace="4"><?=$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> <?
}
?>
使用以下代码打印页面上的主要类别。
<?=$category_box_content;?>