我有一个显示类别和子类别列表的表格,使用函数循环遍历父/子树。这是脚本的标记:
<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>
<?php
function category_list( $category_parent_id = 0 )
{
// build our category list only once
static $cats;
if ( ! is_array( $cats ) )
{
$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';
$res = tep_db_query( $sql );
$cats = array();
while ( $cat = tep_db_fetch_array( $res ) )
{
$cats[] = $cat;
}
}
// populate a list items array
$list_items = array();
foreach ( $cats as $cat )
{
// if not a match, move on
if ( ( int ) $cat['parent_id'] !== ( int ) $category_parent_id )
{
continue;
}
// open the list item
$list_items[] = '<tr class="dataTableRow">';
$list_items[] = '<td class="dataTableContent"><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></td>';
$list_items[] = '</tr>';
}
// convert to a string
$list_items = implode( '', $list_items );
// if empty, no list items!
if ( '' == trim( $list_items ) )
{
return '';
}
// ...otherwise, return the list
return '<ul>' . $list_items . '</ul>';
}
echo category_list();
?>
<td class="dataTableContent"></td>
</table>
目前,这当前正确打印<tr class="dataTableHeadingRow">
和<td class="dataTableHeadingContent">
,但对于<td class="dataTableContent">
,它仅正确打印函数中的标签。如何正确打印两个 dataTableContent 标签,并将它们都保持在循环中?