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 9018615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:34:21+00:00 2026-06-16T04:34:21+00:00

I am using this code snippet to get all category items from mySql db

  • 0

I am using this code snippet to get all category items from mySql db and list them. As you guess, there are main categories and their childs such as Category A and it childs / descendants, then Category B and its childs / descendants and so on.

Now I like to create a HTML table for ever Category- say with 4 cols – and put each category and its childs into a table such as:

UPDATE

    Html Table 1

  Category A    Child3      Child6   Child11
  Child1        Child4      Child7   Child12
  Child 1.1     Child5      Child8   Child13
  Child 1.2     Child 5.1   Child9
  Child2        Child 5.2   Child10

This is the code snippet I use to get list of categories and childs. It lists all categories and subcategories correctly. Now I like to show list in HTML table with columns. Here I am stuck. How can I accomplish this?

        $categoryArr = Array();

        while($categoryRow = mysql_fetch_array($category_query_result)){
            $categoryArr[] = array('parentid'=>$categoryRow['parent_id'],
            'childof'=>$categoryRow['child_of'],
            'categorytitle'=>$categoryRow['category_title'],
            'categoryfilename'=>$categoryRow['category_file_name'],
            'level'=>$categoryRow['level_num']);
        }



        function display_categories($Arr,$thisparent,$level){
            foreach ($Arr AS $key => $catNode ){
                if($catNode['childof']=="$thisparent"){
                    echo "" .$catNode['categorytitle'] ."<br>";

                      display_categories($Arr, $catNode['parentid'] ,$level+1);

                } 

            }

        }

        display_categories($categoryArr,0,1);
  • 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-16T04:34:22+00:00Added an answer on June 16, 2026 at 4:34 am

    Update:

    $categoryArr=array(array("parentid"=>1,"title"=>"Father 1","childof"=>0),array("parentid"=>2,"title"=>"Son 1.2","childof"=>1),array("parentid"=>3,"title"=>"Grandson 1.2.3","childof"=>2),array("parentid"=>4,"title"=>"Son 1.4","childof"=>1),array("parentid"=>5,"title"=>"Son 1.5","childof"=>1),array("parentid"=>6,"title"=>"Son 1.6","childof"=>1),array("parentid"=>7,"title"=>"Grandson 1.5.7","childof"=>5),array("parentid"=>8,"title"=>"Father 8","childof"=>0),array("parentid"=>9,"title"=>"Son 8.9","childof"=>8),array("parentid"=>10,"title"=>"Grandgrandson 1.2.3.10","childof"=>3));
    function rebuild($data,$find)
    {
        $cache=array_filter($data,create_function('$n','return $n["childof"]=='.$find.';'));
        $rt=array();
        foreach($cache as $node)
        {
            $rt[]=$node;
            $rt=array_merge($rt,rebuild($data,$node["parentid"]));
        }
        return $rt;
    }
    $cache=array_filter($categoryArr,function($n){return $n["childof"]==0;});
    $base=array();
    foreach($cache as $root)
    {
        $base[]=array_merge(array($root),rebuild($categoryArr,$root["parentid"]));
    }
    $xml=new DOMDocument("1.0","UTF-8");
    $xml->formatOutput=true; // just to debug
    $root=$xml->createElement("root");
    $xml->appendChild($root);
    foreach($base as $list)
    {
        $table=$xml->createElement("table");
        $root->appendChild($table);
        $rows=ceil(count($list)/4);//change 4 to whatever column you need
        foreach(range(1,$rows) as $idx)
        {
            $tr=$xml->createElement("tr");
            $table->appendChild($tr);
        }
        $cur=0;
        foreach($list as $node)
        {
            if($cur>=$rows) $cur=0;
            $td=$xml->createElement("td");
            $td->appendChild($xml->createTextNode($node["title"]));
            $table->getElementsByTagName("tr")->item($cur)->appendChild($td);
            $cur++;
        }
    }
    echo $xml->saveXML();
    

    Outputs:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
      <table>
        <tr>
          <td>Father 1</td>
          <td>Grandson 1.2.3</td>
          <td>Son 1.4</td>
          <td>Grandson 1.5.7</td>
        </tr>
        <tr>
          <td>Son 1.2</td>
          <td>Grandgrandson 1.2.3.10</td>
          <td>Son 1.5</td>
          <td>Son 1.6</td>
        </tr>
      </table>
      <table>
        <tr>
          <td>Father 8</td>
          <td>Son 8.9</td>
        </tr>
      </table>
    </root>
    

    P.S. Using DOMDocument isn’t a must; Since we already know how many items per table, it’s possible to just calculate the position on each table cell and directly output it, but to me, DOMDocument is lazier 🙂


    First attempt:

    Might not be an elegant solution, but it should work:

    $categoryArr=array(array("parentid"=>1,"title"=>"Father","childof"=>0),array("parentid"=>2,"title"=>"Son 2","childof"=>1),array("parentid"=>3,"title"=>"Grandson 2.3","childof"=>2),array("parentid"=>4,"title"=>"Son 4","childof"=>1),array("parentid"=>5,"title"=>"Son 5","childof"=>1),array("parentid"=>6,"title"=>"Son 6","childof"=>1),array("parentid"=>7,"title"=>"Grandson 5.6","childof"=>5));
    $xml=new DOMDocument("1.0","UTF-8");
    $xml->formatOutput=true; // just to debug
    $root=$xml->createElement("table");
    $xml->appendChild($root);
    foreach(range(1,ceil(count($categoryArr)/4)) as $idx) // change 4 to whatever column you need
    {
        $tr=$xml->createElement("tr");
        $root->appendChild($tr);
    }
    function addNode(&$xml,$data,$currID,&$currRow)
    {
        if($currRow>=$xml->getElementsByTagName("tr")->length) $currRow=0;
        $cache=array_filter($data,create_function('$n','return $n["parentid"]=='.$currID.';'));
        if(count($cache)==1)
        {
            $td=$xml->createElement("td");
            $cache=array_pop($cache);
            $td->appendChild($xml->createTextNode($cache["title"]));
            $xml->getElementsByTagName("tr")->item($currRow)->appendChild($td);
            $currRow+=1;
        }
        $cache=array_filter($data,create_function('$n','return $n["childof"]=='.$currID.";"));
        foreach($cache as $node)
        {
            addNode($xml,$data,$node["parentid"],$currRow);
        }
    }
    $cur=0;
    addNode($xml,$categoryArr,0,$cur);
    echo $xml->saveXML();
    

    outputs:

    <?xml version="1.0" encoding="UTF-8"?>
    <table>
      <tr>
        <td>Father</td>
        <td>Grandson 2.3</td>
        <td>Son 5</td>
        <td>Son 6</td>
      </tr>
      <tr>
        <td>Son 2</td>
        <td>Son 4</td>
        <td>Grandson 5.6</td>
      </tr>
    </table>
    

    No “real” data to test, so let me know if there’s something wrong.

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

Sidebar

Related Questions

I'm using this snippet of code to output a list of all the computers
I am using this code snippet to add KeyDown event handler to any element
In the tableView:cellForRowAtIndexPath: implementation of my UITableViewController subclass I'm using this code snippet: AlarmPicker
I currently am using this JavaScript code snippet to select 3 checkboxes at a
I have the following code snippet: using (SPSite site = new SPSite(this.ListAddress)) { using
I am using the following code snippet to calculate a total price. This works
This is the snippet of code i'm using now: def countOccurences(a, b) #counts the
I'm trying to execute a command using the following code snippet: foreach($package in (Get-Childitem
Using this code, i am able to send a notification to my own device.
Using this code to zip a folder and it works perfect on small files

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.