patternphpMinor
Rendering nested array in a <ol> with headings
Viewed 0 times
arraywithheadingsnestedrendering
Problem
I have an array with the following structure
I'm trying to render this array in the following way
Area ABC
And am currently using the following code to achieve this
Is this the best way of doing it, or is there a better way, I will have more areas and activities, but the structure stays the same.
Array
(
[0] => Array
(
[area] => Area ABC
[activity] => Array
(
[0] => Activity 1
[1] => Activity 2
)
)
)I'm trying to render this array in the following way
Area ABC
- Activity 1
- Activity 2
And am currently using the following code to achieve this
$status = 'closed';
foreach($suggestions as $suggestion)
{
if($currentarea != $suggestion['area'])
{
echo "";
echo $suggestion['area'];
echo "";
if($status == 'closed')
{
echo "";
$status = 'open';
}
}
foreach($suggestion['activity'] as $activity)
{
echo "";
echo $activity;
echo "";
}
if($status == 'open')
{
echo "";
$status = 'closed';
}
$currentarea = $suggestion['area'];
}Is this the best way of doing it, or is there a better way, I will have more areas and activities, but the structure stays the same.
Solution
This won't work if each suggestion does not start a new area.
You close on every iteration that it's open and add list items on every iteration, but you only open if the area changes.
This code is simpler and will work more reliably.
This is the wrong place to discuss how to write new code. But if you want to get the previous concept working, you'll want to open and close inside the
foreach($suggestions as $suggestion)
{
if($currentarea != $suggestion['area'])
{
echo "";
echo $suggestion['area'];
echo "";
if($status == 'closed')
{
echo "";
$status = 'open';
}
}
foreach($suggestion['activity'] as $activity)
{
echo "";
echo $activity;
echo "";
}
if($status == 'open')
{
echo "";
$status = 'closed';
}
$currentarea = $suggestion['area'];
}You close on every iteration that it's open and add list items on every iteration, but you only open if the area changes.
foreach($suggestions as $suggestion)
{
echo '';
echo $suggestion['area'];
echo "\n";
echo "\n";
foreach($suggestion['activity'] as $activity)
{
echo ' ';
echo $activity;
echo "\n";
}
echo "\n";
}This code is simpler and will work more reliably.
This is the wrong place to discuss how to write new code. But if you want to get the previous concept working, you'll want to open and close inside the
if that checks if the area has changed. Or pull off the first element of the list so that you can treat it differently. Or make a new list where there's only one suggestion per area, combining multiple suggestions with the same area into one.Code Snippets
foreach($suggestions as $suggestion)
{
if($currentarea != $suggestion['area'])
{
echo "<h3>";
echo $suggestion['area'];
echo "</h3>";
if($status == 'closed')
{
echo "<ol>";
$status = 'open';
}
}
foreach($suggestion['activity'] as $activity)
{
echo "<li>";
echo $activity;
echo "</li>";
}
if($status == 'open')
{
echo "</ol>";
$status = 'closed';
}
$currentarea = $suggestion['area'];
}foreach($suggestions as $suggestion)
{
echo '<h3>';
echo $suggestion['area'];
echo "</h3>\n";
echo "<ol>\n";
foreach($suggestion['activity'] as $activity)
{
echo ' <li>';
echo $activity;
echo "</li>\n";
}
echo "</ol>\n";
}Context
StackExchange Code Review Q#95722, answer score: 3
Revisions (0)
No revisions yet.