这里给出分离可用和不可预订的汽车列表的解决方案。它对您非常有用。
获取可供预订的车辆列表。IE)。可预订特定日期间隔..
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
获取不可预订的汽车列表。IE)。已为特定日期间隔保留..
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
注意:将日期转换为时间戳格式..
$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);
代码如下:
<?php
$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);
$carstringAvailable = mysql_query("select *,'available' as status from cars
WHERE CarID not 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");
$getcarsAvailable = $carstringAvailable;
?>
<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>