0

我有一个显示类别和子类别列表的表格,使用函数循环遍历父/子树。这是脚本的标记:

<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; ?>&nbsp;</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>

4

2 回答 2

0

首先,从 中删除 php 函数<td>并将其放在 table/html 的顶部。而不是echo category_list();,做$list = category_list();

然后在 HTML 中,执行以下操作:

       <tr class="dataTableRow">
       <?php foreach( $list as $v) { ?>           
         <td class="dataTableContent"><?php echo $v; ?></td> 
         <?php } ?>

最新编辑:

       <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; ?>&nbsp;</td>
</tr>
<tr class="dataTableRow"><ul> <!-- change #1 -->

<?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>'; Change #2

      foreach ( $cats as $cat )
     {
    // open the list item
    $list_items[] = '<td class="dataTableContent"><li>'; //Change #3

    // 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>'; //Change #4
}
$list_items[] = '</ul></tr>'; //Change #5

// convert to a string
return implode( '', $list_items );

}  

echo category_list();
?>

 <!--remove </tr> change #6 -->
 </table>
于 2012-09-26T16:02:51.210 回答
0

在这里,我为你清理了这个。特别是,每个函数实际上应该只做一件事,因此您可以直接了解它在做什么。我还删除了关于你的列表/内爆事情的不必要的错误。你正在输出一个字符串,使用一个字符串!

<?php
/**
  * Gets all categories for a particular parent id.
  */
function get_categories( $category_parent_id = 0 ) {
    $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;
    $result  = tep_db_query( $sql );//Really should use PDO
    $categories = array();//Categories are not chased by dogs. Use full names for your variables!

    while ( $category = tep_db_fetch_array( $result ) )
    {
        $categories[] = $category;
    }

    return $categories;
}

/**
 * Outputs HTML for a list of categories, recursing into them if necessary for more data.
 */
function output_categories($categories, &$output = "") {
    if (count($categories) == 0 || empty($categories)) { return; }//either works.

    $output =. "<ul>";//change this to <tr> for a table row
    foreach ($categories as $category) {
        output =. "<li>";//change this to <td> for a table cell
        $category['categories_name'];
        output_categores(categories($category['categories_id']), $output);
        output =. "</li>";//change to </td>
    }
    $output =. "</ul>";//change to </tr>
    return;//Note that $output will be changed by this function.
}
%>
<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; ?>&nbsp;</td>
    </tr>
    <tr class="dataTableRow">
        <td class="dataTableContent">
        <?php
            echo output_categories(get_categories());
        ?>
        </td>
        <td class="dataTableContent"></td>
    </tr>
</table>
于 2012-09-26T16:20:18.150 回答