【发布时间】:2011-08-15 06:09:32
【问题描述】:
我得到了这个脚本,它在这个表中查找所用的时间,然后从这三个数组中删除这些时间。数组是 1-24 倍。
此脚本的最终目标是比较这些数组中所有缺失的时间,并制作一个只有可用时间的大数组。
关键是它需要检查一个时间是否连续 3 次丢失。如果是,则该时间不会显示在最终数组中。
例如:
<?php
include 'db-connect.php';
if (isset($_GET['month']) && isset($_GET['day']) && isset($_GET['year'])) {
$month = $_GET['month'];
$day = $_GET['day'];
$year = $_GET['year'];
//string together date
$date = $month."/".$day."/".$year;
//define the queries
$sql1 = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '1'");
$sql2 = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '2'");
$sql3 = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date' AND server = '3'");
//define time lists for each server
$timelist1 = range(1, 24);
$timelist2 = range(1, 24);
$timelist3 = range(1, 24);
//unset the arrays with the taken times for server 1
while($query1 = mysql_fetch_array($sql1)) {
unset($timelist1[$query1['start_time'] - 1]);
}
//unset the arrays with the taken times for server 2
while($query2 = mysql_fetch_array($sql2)) {
unset($timelist2[$query2['start_time'] - 1]);
}
//unset the arrays with the taken times for server 3
while($query3 = mysql_fetch_array($sql3)) {
unset($timelist3[$query3['start_time'] - 1]);
}
//now see which times are missing three times in a row and make one final array of available times.
//code goes here...
}
?>
【问题讨论】:
-
您不需要为每个服务器值运行单独的查询。您也不需要(或不想)在查询中为数字加上引号。
标签: php mysql arrays time compare