Please advise me for best way to loop this data.
I have data in my database like this
$data = [
['id' => 1, 'name' => 'Category A', 'parent_id' => null],
['id' => 2, 'name' => 'Subcategory A.1', 'parent_id' => 1],
['id' => 3, 'name' => 'Subcategory A.1.1', 'parent_id' => 2],
['id' => 4, 'name' => 'Subcategory A.1.2', 'parent_id' => 2],
['id' => 5, 'name' => 'Category B', 'parent_id' => null],
['id' => 6, 'name' => 'Subcategory B.1', 'parent_id' => 5],
['id' => 7, 'name' => 'Subcategory B.1.1', 'parent_id' => 6],
['id' => 8, 'name' => 'Subcategory B.1.2', 'parent_id' => 6],
['id' => 9, 'name' => 'Subcategory A.1.3', 'parent_id' => 2],
['id' => 10, 'name' => 'Subcategory A.2', 'parent_id' => 1],
['id' => 11, 'name' => 'Subcategory A.2.1', 'parent_id' => 10],
];
and I want to wrap the loop in a div tag like this
<div class="card">
<div class="header">Category 1</div>
<div class="column-start">Subcategory A.1</div>
<div class="column">Subcategory A.1.1</div>
<div class="column">Subcategory A.1.2</div>
<div class="column">Subcategory A.1.3</div>
<div class="column-start">Subcategory A.2</div>
<div class="column">Subcategory A.2.1</div>
<div class="header">Category 2</div>
<div class="column-start">Subcategory B.2</div>
<div class="column">Subcategory B.2.1</div>
<div class="column">Subcategory B.2.2</div>
</div>
I want to do a loop so I get a result like this
enter image description here
2
Answers
Try to solve problems on your own for at-least 20 minutes and then post a question here with the code that you’ve tried. However here is the solution:
Try to below solution and if you’ll face any issue comment here: