我有一个显示类别和子类别列表的表格,使用函数循环遍历父/子树。这是脚本的标记:
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_WEIGHT; ?> </td>
</tr>
<tr class="dataTableRow">
<td class="dataTableContent">
<?php
function category_list( $category_parent_id = 0 )
{
// NOTE THE ADDITIION OF THE PARENT ID:
$sql = 'select cd.categories_name,c.categories_id, c.parent_id, c.sort_order from ' . TABLE_CATEGORIES . ' c, ' . TABLE_CATEGORIES_DESCRIPTION . ' cd where c.categories_id = cd.categories_id AND c.parent_id='.$category_parent_id;
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
if (count($cats) == 0)
{
return '';
}
// populate a list items array
$list_items = array();
$list_items[] = '<ul>';
foreach ( $cats as $cat )
{
// open the list item
$list_items[] = '<li>';
// construct the category link
$list_items[] = $cat['categories_name'];
// recurse into the child list
$list_items[] = category_list( $cat['categories_id'] );
// close the list item
$list_items[] = '</li>';
}
$list_items[] = '</ul>';
// convert to a string
return implode( '', $list_items );
}
echo category_list();
?>
</td>
<td class="dataTableContent"></td>
</tr>
</table>
<td>
我将如何打印单个列表元素,而不是打印为一个列表<td>
?