Reputation: 729
I have two arrays, one for creating the first column (service) in a table like structure and the second for every other column (day) after that. First column array has the structure:
Array
(
[0] => DPW
[1] => PNS
[2] => WS1
[3] => WS2
)
This is a dynamic array so there could be more values than this, but it will always be unique and sorted alphabetically.
My second array for the other columns has the structure:
Array
(
[0] => Array
(
[Monday] => Array
(
[0] => Array
(
[date] => 42016
[message] => 07:00 START
[service] => DPW
)
[1] => Array
(
[date] => 42016
[message] => 16:30 START
[service] => PNS
)
[2] => Array
(
[date] => 42016
[message] =>
[service] => WS1
)
// etc ...
[3] => Array
(
[Thursday] => Array
(
[0] => Array
(
[date] => 42017
[message] =>
[service] => DPW
)
[1] => Array
(
[date] => 42017
[message] => 16:00 CUT OFF
[service] => DPW
)
[2] => Array
(
[date] => 42017
[message] =>
[service] => PNS
)
)
)
)
So basically each day is the next column. My problem is I initially thought that the service key in the second array was always in the same order for each day, but its not. You can see from the Thursday that DPW is twice in a row, whereas Monday isn't. I can change the format of these arrays, so this is where my questions lie.
My thoughts are for starters the day should contain the same number of keys as the services array, so should I add a blank value to make up the same number of keys? To cater for the same service in a row should I add a further level to cater for multiple values look like so:
Array
(
// etc ...
[3] => Array
(
[Thursday] => Array
(
[0] => Array
(
[0] => Array
(
[date] => 42017
[message] =>
[service] => DPW
)
[1] => Array
(
[date] => 42017
[message] =>
[service] => DPW
)
)
[2] => Array
(
[0] => Array
(
[date] => 42017
[message] =>
[service] => PNS
)
)
[3] => Array
(
)
[4] => Array
(
)
)
)
)
I have also added in the blank values. Finally how would I print these values in the same order as the first (services) array so they aren't under the wrong service?
If is difficult to explain this and I could be going about it the wrong way, but that's why I am asking the questions.
EDIT:
Almost there with this, my only issue now is I need to show the Start and Cut Off date as being the same item spread out across multiple days. For instance the array might have these values at some point:
[1] => Array
(
[date] => 42016
[message] => 16:30 START
[service] => DPM
)
// etc...
[3] => Array
(
[date] => 42021
[message] => 16:30 CUT OFF
[service] => DPM
)
So I need a way to have these on their own row with a background colour or something to show they are the one item, like the graphic above. Not sure if this is possible?
Thanks Robert!
Upvotes: 1
Views: 87
Reputation: 2204
I'm not sure this is exactly what you want, but it might help :
$services = array('DPW', 'PNS', 'WS1', 'WS2');
$days = array(
array(
'Monday' => array(
array(
'date' => 42016,
'message' => '07:00 START',
'service' => 'DPW'
),
array(
'date' => 42016,
'message' => '16:30 START',
'service' => 'PNS'
),
array(
'date' => 42016,
'message' => '',
'service' => 'WS1'
),
),
),
array(
'Thursday' => array(
array(
'date' => 42017,
'message' => '',
'service' => 'DPW'
),
array(
'date' => 42017,
'message' => '16:00 CUT OFF',
'service' => 'DPW'
),
array(
'date' => 42017,
'message' => '',
'service' => 'PNS'
)
)
)
);
And here is the HTML Table:
<?php $service_days = []; ?>
<table cellpadding="10" width="100%" align="left">
<thead>
<td style="background:#333;color:#FFF">Service</td>
<?php foreach($days as $day): ?>
<td style="background:#666;color:#FFF"><?php echo array_keys($day)[0]; ?></td>
<?php
foreach(array_values($day)[0] as $d)
$service_days[array_keys($day)[0]][$d['service']][] = $d;
?>
<?php endforeach; ?>
</thead>
<?php foreach($services as $service): ?>
<tr>
<td><?php echo $service ?></td>
<?php foreach($service_days as $day => $srv): ?>
<td>
<?php foreach($srv as $_service => $periode): ?>
<?php if($_service == $service): ?>
<?php foreach($periode as $p): ?>
<p style="background:#DDD"><?php echo $p['date'], '<br>', $p['message']; ?></p>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Upvotes: 1