php - Displaying the schedule with multiple foreach. How to improve code and reduce foreach loops?
Get the solution ↓↓↓I was trying to make a schedule from the database that would look something like this:
I ended up using three loops for displaying. First for showing dates, second to separate every day into rows with overlapping events and third to print out the events in a row.
Before display I had two arrays:$overlap_array
and$dates_unique
.$overlap_array
is the array with resolved rows with events that are overlapping. Here is the little snippet from$overlap_array
:
array (size=7)
0 =>
array (size=2)
0 =>
array (size=7)
0 => string '2014-04-21' (length=10)
1 => string '10:00:00' (length=8)
2 => string '14:00:00' (length=8)
3 => string '36000' (length=5)
4 => string '14400' (length=5)
5 => string 'Listening Cities Presentations Session 1' (length=40)
6 => string '1' (length=1)
1 =>
array (size=7)
0 => string '2014-04-21' (length=10)
1 => string '14:00:00' (length=8)
2 => string '20:30:00' (length=8)
3 => string '50400' (length=5)
4 => string '23400' (length=5)
5 => string 'Listening Cities Presentation 2' (length=31)
6 => string '2' (length=1)
1 =>
array (size=3)
0 =>
array (size=7)
0 => string '2014-04-21' (length=10)
1 => string '15:30:00' (length=8)
2 => string '17:00:00' (length=8)
3 => string '55800' (length=5)
4 => string '5400' (length=4)
5 => string 'Listening Cities Performance at the DockBar: Part1' (length=50)
6 => string '3' (length=1)
And$dates_unique
is an array with unique dates in the schedule:
array (size=3)
0 => string '2014-04-21' (length=10)
6 => string '2014-04-22' (length=10)
9 => string '2014-04-23' (length=10)
I added Tab.js from Twitter Bootsrap 3 for my tabs. Then I used those arrays to display the schedule inside 3 foreach loops:
echo '<div class="tab-content">';
$a = 1;
$class_active = 'tab-pane fade in active';
foreach ($dates_unique as $dates) {
echo '<div id="section' . $a . '" class="' . $class_active . '">';
$a++;
$class_active = "tab-pane fade";
foreach ($overlap_array as $row) {
if ($dates == $row[0][0]) {
echo '<div class="rows">';
foreach ($row as $event) {
$left = ((($event[3] - 36000) / 468)) + 0.1;
echo '<a href="event/' . $event[6] . '"><div style="width:' .
(($event[4] / 468) - 0.4) .'%; left: ' . $left . '%;" class="collumn">'.
$event[5] . ' ' . $event[1] . ' - ' . $event[2] .' '. $event[0] .
'</div></a>';
}
echo '</div>';
}
}
echo '</div>';
}
echo '</div>';
How could I reduce the number of foreach loops here or maybe I should use another way to display the schedule?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: object not found by the @paramconverter annotation.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.