Here is a solution for a separate list of cars available and not available for booking. This is very useful for you.
Get a list of available cars for booking. those.). Reservations are available for a specific date range.
select *,'available' as status from cars where CarID not in(SELECT CarID FROM reservations WHERE UNIX_TIMESTAMP( CONCAT( startDate, ' ', startTime ) ) <= $startTimestamp AND UNIX_TIMESTAMP( CONCAT( endDate, ' ', endTime ) ) >= $endTimestamp)WHERE deleted = 'no' group by make,model order by make,model asc
Get a list of available cars for booking. those.). already reserved for a specific date range.
select *,'not available' as status from cars where CarID not in(SELECT CarID FROM reservations WHERE UNIX_TIMESTAMP( CONCAT( startDate, ' ', startTime ) ) <= $startTimestamp AND UNIX_TIMESTAMP( CONCAT( endDate, ' ', endTime ) ) >= $endTimestamp)WHERE deleted = 'no' group by make,model order by make,model asc
Note: convert the date to a timestamp format.
$startDateTime="21/04/2013 13:00:00"; //date example list($sDay, $sMonth, $sYear, $sHour, $sMinute,$sSeconds) = split('[/ :]', $startDateTime); $startTimestamp=mktime($sHour, $sMinute,$sSeconds, $sMonth, $sDay, $sYear); $endDateTime="29/04/2013 13:00:00"; //date example list($eDay, $eMonth, $eYear, $eHour, $eMinute,$eSeconds) = split('[/ :]', $endDateTime); $endTimestamp=mktime($eHour, $eMinute,$eSeconds, $eMonth, $eDay, $eYear);
and the code is below:
<?php $startDateTime="21/04/2013 13:00:00"; <h3>Available cars for reservation</h3> <table> <tr> <th>CarID</th><th>make</th><th>model</th><th>serial</th><th>image</th><th>status</th> </tr> <?php if(mysql_num_rows($getcarsAvailable)>0) { while($searchcarsAvail = mysql_fetch_array($getcarsAvailable)) { ?> <tr> <td><?php echo $searchcarsAvail['CarID'];?></td> <td><?php echo $searchcarsAvail['make'];?></td> <td><?php echo $searchcarsAvail['model'];?></td> <td><?php echo $searchcarsAvail['serial'];?></td> <td><?php echo $searchcarsAvail['image'];?></td> <td><?php echo $searchcarsAvail['status'];?></td> </tr> <?php } } else { ?> <tr> <td colspan='5'>No records found</td> </tr> <?php } ?> </table> <?php $carstringUnAvailable = mysql_query("select *,'available' as status from cars WHERE CarID in( SELECT CarID FROM reservations WHERE UNIX_TIMESTAMP( CONCAT( startDate, ' ', startTime ) ) <= $start_Timestamp AND UNIX_TIMESTAMP( CONCAT( endDate, ' ', endTime ) ) >= $end_Timestamp ) AND deleted = 'no' group by make,model order by make,model asc"); $getcarsUnAvailable = $carstringUnAvailable; ?> <h3>Already Reserved cars list</h3> <table> <tr> <th>CarID</th><th>make</th><th>model</th><th>serial</th><th>image</th><th>status</th> </tr> <?php if(mysql_num_rows($getcarsUnAvailable)>0) { while($searchcarsUnAvail = mysql_fetch_array($getcarsUnAvailable)) { ?> <tr> <td><?php echo $searchcarsUnAvail['CarID'];?></td> <td><?php echo $searchcarsUnAvail['make'];?></td> <td><?php echo $searchcarsUnAvail['model'];?></td> <td><?php echo $searchcarsUnAvail['serial'];?></td> <td><?php echo $searchcarsUnAvail['image'];?></td> <td><?php echo $searchcarsUnAvail['status'];?></td> </tr> <?php } } else { ?> <tr> <td colspan='5'>No records found</td> </tr> <?php } ?> </table>