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

ここで、私はあなたのためにこれを片付けました。特に、各関数は実際には 1 つのことだけを実行する必要があるため、その実行内容を正確に保つことができます。また、あなたの list/implode に関する不必要な誤解を取り除きました。文字列を出力しています。文字列を使用してください。

<?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 に答える