Hello:
I am stuck with a bunch of foreach loops and would like to know if there is a way to hone them down to a simple 'for' loop or a recursive function? I'm trying to generate HTML with the elements nested inside each other. I guess what I'm trying to get at is an arrays of arrays. But I don't know how to move forward with what I've created so far. Can someone please help me to make this monster into something more tamable? Thank you!
Here's my code:
$containers = DISPLAY::displayParentElements($data);
$subcontainers = DISPLAY::displayChildElements($data2);
foreach($containers as $parent) {
$parentDiv = $parent['parentDiv'];
echo '<div id="'.$parentDiv.'">';
foreach($subcontainers as $child) {
echo '<div id="'.$child['childDiv'].'">';
foreach($subcontainers as $grandChild) {
echo '<div id="'.$grandChild['grandChildDiv'].'">';
foreach($subcontainers as $greatGrandChild) {
echo '<div id="'.$greatGrandChild['greatGrandChildDiv'].'">';
echo '</div>';
}
echo '</div>';
}
echo '</div>';
}
echo '</div>';
}
The results will then look like this:
<div id="siteContainer">
<div id="header">
<div id="logoContainer">/div>
<div id="logo"></div>
<div id="links"></div>
<div id="contactInfo">
<div id="logoText">
<div id="shortDiv">
<div class="headerText"></div>
</div>
</div>
</div>
</div>
<div id="body">
<div id="longDiv"></div>
<div id="greetings"></div>
</div>
<div>
The $containers
array has the following info:
Array
(
[attribute_value] => siteContainer
)
Array
(
[attribute_value] => header
)
Array
(
[attribute_value] => logoContainer
)
Array
(
[attribute_value] => logo
)
Array
(
[attribute_value] => logoText
)
Array
(
[attribute_value] => links
)
Array
(
[attribute_value] => contactInfo
)
Array
(
[attribute_value] => body
)
Array
(
[attribute_value] => longDiv
)
Array
(
[attribute_value] => shortDiv
)
Array
(
[attribute_value] => headerText
)
Array
(
[attribute_value] => greetings
)
The $subcontainers
array has pretty much the same info but with an extra key:
Array
(
[parent_container_name] => siteContainer
[attribute_value] => header
)
Array
(
[parent_container_name] => header
[attribute_value] => logoContainer
)
Array
(
[parent_container_name] => header
[attribute_value] => logo
)
Array
(
[parent_container_name] => contactInfo
[attribute_value] => logoText
)
Array
(
[parent_container_name] => header
[attribute_value] => links
)
Array
(
[parent_container_name] => header
[attribute_value] => contactInfo
)
Array
(
[parent_container_name] => siteContainer
[attribute_value] => body
)
Array
(
[parent_container_name] => body
[attribute_value] => longDiv
)
Array
(
[parent_container_name] => logoText
[attribute_value] => shortDiv
)
Array
(
[parent_container_name] => shortDiv
[attribute_value] => headerText
)
Array
(
[parent_container_name] => body
[attribute_value] => greetings
)
I'm pretty sure the two arrays could be narrowed down into one or by only using the $containers
array.