[eluser]gRoberts[/eluser]
The best way, or at least the way I would do this, is to return an array of vehicles, which will be your basis for your matrix.
i.e. if there were no vehicles rented out, this would simply show available for all vehicles.
Then, I would query the database for all vehicles that are rented out between the two dates.
i.e
Code:
<?
function get_vehicle_usage($From, $To)
{
$_From = strtotime($From);
$_To = strtotime($To);
$this->db->where('StartDate >=', $_From);
$this->db->where('EndDate <=', $_To);
$q = $this->db->get('vehicleusage');
$ret = array();
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$start = $row->StartDate;
while($start < $row->EndDate)
{
$ret[$row->VehicleID][] = date('d-m-Y', $tmp);
$start = strtotime('+1 day', $start);
}
}
}
$q->free_result();
return $ret;
}
?>
The above would return an multi-dimensional array, which the index would contain the VehicleID and the value would be an array of dates that vehicle is not available.
Then, I would do the following in your view:
Code:
<table id="diary">
<thead>
<tr>
<td><strong>VEHICLE REG</strong></td>
<?php foreach($dates as $date) {?>
<td><?=$date?></td>
<?php }?>
</tr>
</thead>
<tbody>
<?php foreach($vehicles as $vehicle) { ?>
<tr>
<td><?=$vehicle->registration;?></td>
<?php foreach($dates as $date) {?>
<td><?= (isset($VehicleUsage[$vehicle->VehicleID]) ? (in_array($date, $VehicleUsage[$vehicle->VehicleID]) ? 'Unavailable' : 'Available') : 'Available'); ?></td>
<?php }?>
</tr>
<?php }?>
</tbody>
</table>