Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:48:43+00:00 2026-06-11T22:48:43+00:00

I have a table showing the list of categories and subcategories, using a function

  • 0

I have a table showing the list of categories and subcategories, using a function to loop through the parent/child tree. Here is the markup of the script:

<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>

<?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>

At the moment this currently prints the <tr class="dataTableHeadingRow"> and both the <td class="dataTableHeadingContent">correctly, but for the <td class="dataTableContent">, it is only printing the tag in the function correctly. How would I print both the dataTableContent tags correctly, and keep them both in the loop?
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:48:44+00:00Added an answer on June 11, 2026 at 10:48 pm

    Looking at your recursive use of the function category_list you will be outputting <tr> elements inside <li>s. This is invaild HTML.

    Also, each <tr> in the body will only have one cell, whereas the <tr> in the header row has two cells.

    You should consider how the output should look, then write your code to account for that, and follow the flow through to see what is being written out. Also, check the actual HTML source returned to your browser, not how your browser renders it. Note that different browsers will render invalid HTML differently.

    Also, (not related to your problem) consider replacing the border, width, cellspacing, cellpadding and align HTML attributes with CSS instead. Another consideration would be to use the <head> and <tbody> elements as additional semantic markup.

    Further Explanation

    Your problem is essentially these lines:

        // 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>';
    

    Every iteration of your loop is creating a new tr element! Try this instead:

    <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)
       {
          // There are no categories to list
          return '';
       }
    
        // Create a list HTML string
        $list = '<ul>';
    
        foreach ( $cats as $cat )
        {
            // open the list item
            $list .= '<li>';
    
            // construct the category link
    
            $list .= $cat['categories_name'];
    
            // recurse into the child list
            $list .= category_list( $cat['categories_id'] );
    
            // close the list item
            $list .= '</li>';
        }
    
        // close and return the list
        $list .= '</ul>';
        return $list;
    
    }  
    
    echo category_list();
    ?>
            </td>
            <td class="dataTableContent"></td>
        </tr>
    </table>
    

    Note that further improvements can be made regarding data and presentation logic separation. You don’t necessarily need to adopt a full-scale MVC framework; just use OOP to encapsulate your SQL queries within classes which represent your database entities.

    EDIT

    Following the specification that each category should be in its own <td>.

    This requires the removal of the list elements. A <ul> can only contain <li> elements as children; it cannot contain <tr> children. A <td> MUST be a child of a <tr>. It is not allowed anywhere else. A <tr> can only be a child of a <thead>, <tbody>* or <tfoot>. These rules are all defined in the HTML DTD.

    Therefore, the following is the sort of thing you need to be looking at:

    <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>
    <?php
    function category_list( $category_parent_id = 0, $level = 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;
        }
    
        $list = '';
        foreach ( $cats as $cat )
        {
            // start the next row:
            $list .= "<tr class=\"dataTableRow\">\n";
            // The cell for the category needs
            $list .= "<td class=\"dataTableContent\">\n";
    
            // construct the category link.  Note we are now enclosing 
            // this in a div with a left-indent to show the sub level
            // this category is at.  Adjust the padding-left calculation 
            // to suit your page
            $list .= '<div style="padding-left: ' . (2 * $level) . 'em;">';
            $list .= "&bull; {$cat['categories_name']}";
            $list .= "</div>\n";
    
            // close the row
            $list .= "</td>\n";
            $list .= "<td class=\"dataTableContent\"></td>\n";
            $list .= "</tr>\n";
    
            // recurse into the child list, incrementing $level
            $list .= category_list( $cat['categories_id'], 1+$level );
        }
    
        // return the list
        return $list;
    
    }
    
    echo category_list();
    ?>
    </table>
    

    *Note that <tbody> is an implied element. It doesn’t have to be explicitly defined in the code, but it will always be there in the DOM.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table showing the list of categories and subcategories, using a function
I am using Struts1.3, I have Jsp page which is showing the list of
I have table view which is showing list of files to download. When I
I have a table showing a list of objects; let's call them Employee for
I have a table view that's showing a list of bank transactions. When I
Im showing a table of data using a paginated list, with a search box
When showing data from a mysql table on an php page does it have
I have a table showing information from a mysql table. I have three forms
Suppose I have the following schema: Table: Showing showing_id movie_id theater_id show_date show_time Table:
I'm trying to display a table showing a list of courses. I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.