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);
Update:
Outputs:
P.S. Using
DOMDocumentisn’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,DOMDocumentis lazier 🙂First attempt:
Might not be an elegant solution, but it should work:
outputs:
No “real” data to test, so let me know if there’s something wrong.