php-两次返回所有时间

时间:2013-12-13 23:23:34

标签: php mysql

我正在为iPad制作一个预订系统,我有一个表格,它将开始时间和结束时间发布到处理页面然后调用一个函数,它运行良好,直到我添加了15分钟的增量。这是表格:

<select name='hr_start'>
<option value=""></option>
<option value="08">8AM</option>
<option value="09">9AM</option>
<option value="10">10AM</option>
<option value="11">11AM</option>
<option value="12">12PM</option>
<option value="13">1PM</option>
<option value="14">2PM</option>
<option value="15">3PM</option>
</select>
:<select name='min_start'>
<option value=""></option>
<option value="0">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>

</select>
to
<select name='hr_end'>
<option value=""></option>
<option value="09">9AM</option>
<option value="10">10AM</option>
<option value="11">11AM</option>
<option value="12">12PM</option>
<option value="13">1PM</option>
<option value="14">2PM</option>
<option value="15">3PM</option>
</select>
:<select name='min_end'>
<option value=""></option>
<option value="0">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<input type='hidden' name='day' value='<? echo $day ?>' hidden />
<input type='hidden' name='month' value='<? echo $month ?>' hidden />
<input type='hidden' name='year' value='<? echo $year ?>' hidden />
<input type='hidden' name='type' value='<? echo $type?>' hidden />
<input type="submit" value="Reserve Assets" />
</form> 

这是将上述值放在数组中的代码:

$hour_start = $_POST['hr_start'];
$min_start = $_POST['min_start'];

$hour_end = $_POST['hr_end'];
$min_end = $_POST['min_end'];

$h = array();
$m = array();


for($i=$hour_start; $i<=$hour_end; $i++) {
  $h[] = $i;
}
//array_unshift($h, $hour_start);
//array_push($h, $hour_end);
for($i=$min_start; $i<=$min_end; $i++) {
$m[] = $i;
}
array_unshift($m, $min_start);
array_push($m, $min_end);
array_push($m, $min_end);
$requested = $_POST['requested'];

下面是创建预订记录的功能,$m是月份,$d是一天,$y是年份,$h是小时数组(24小时格式:15 14 12等)和$m是分钟数组(0,15,30,45),$at是资产类型(5位数ID):

function generate_confimation ($m, $d, $y, $h, $m1, $at, $numrequested, $user) {
echo '<pre>';

var_dump($h);
var_dump($m1);
echo '</pre>';
foreach ($h as $index => $value)
{
 $t1 =  $h[$index];
 $t2 = $m1[$index];

//$time = $t;
//echo $time;
//echo $utime;
$numbers = array_map('_getNumber', $h);

$startTime = min($numbers);
$endTime = max($numbers);
//$endTime = $endTime;



$startTime = mktime($startTime, 0, 0, $m, $d, $y);
$endTime = mktime($endTime, 0, 0, $m, $d, $y);
$num_avaliable = num_available($m, $d, $y, $t1, $t2, $at);
if ($numrequested > $num_avaliable) {
$error = 'ERROR NOT ENOUGH ASSETS TO FURFILL REQUEST';
return $error;
}
}
mysql_query(
"INSERT INTO transactions(user, start, end)
VALUES('$user', '$startTime', '$endTime')");
$txnid = mysql_insert_id();





/*
$check1 = mysql_query("SELECT id FROM assets WHERE type_id = '$at'");

$check12 = mysql_num_rows($check1);
echo $numrequested;
if ($numrequested > $check12) {
echo 'ERROR NOT ENOUGH ASSETS TO FURFILL REQUEST';
return;
}
*/

//$x = 0;
$numrequested = $numrequested +1 ;
$numrequested;
foreach($h as $t){
$x = 0;
$time = $t;
$ftime = "$time:00:00";
//echo $ftime;
$utime = mktime($time, 0, 0, $m, $d, $y);
//echo $utime;

$assetfetch = mysql_query("SELECT id FROM assets WHERE type_id = $at") or die('ERROR: THERE WAS A SERVER ERROR<br>Please click <a href="reserve.php"> here</a> and try again and then Contact Your Network Administrator for Assistance<br><br>
There may be additional errors, but the server did not report them');
$numrequested = $numrequested - 1;
while($row = mysql_fetch_array($assetfetch)) {
        $assetid= $row['id'];
        //echo $assetid;
        //echo '<br>';
        $numquery = mysql_query("SELECT *
            FROM reservations
            WHERE asset_id = $assetid AND
            start = '$utime'") or die('ERROR: THERE WAS A SERVER ERROR<br>Please click <a href="reserve.php"> here</a> and try again and then Contact Your Network Administrator for Assistance<br><br>
There may be additional errors, but the server did not report them');
$numfind = mysql_num_rows($numquery);
if ($numfind == '0')  {
$query = 
"INSERT INTO reservations(asset_id, user, start, txnid)
VALUES('$assetid', '$user', '$utime', '$txnid')";
mysql_query($query) or die(mysql_error());
$x++;
if ($x >= $numrequested){
break;
}
}
}






}


return $txnid;

}

我不确定问题出在哪里,但感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

$m不是(0,15,30,45)的数组,因为$i的增量为1:

for($i=$min_start; $i<=$min_end; $i++) {
$m[] = $i;
}

应该是

for($i=$min_start; $i<=$min_end; $i+=15) {
$m[] = $i;
}