2013-05-12 54 views
-2
$day1=date('d'); 
$add2 = strtotime("+ 1 day"); 
$day2=date('d', $add2); 
$add3 = strtotime("+ 2 day"); 
$day3=date('d', $add3); 
$add4 = strtotime("+ 3 day"); 
$day4=date('d', $add4); 
$add5 = strtotime("+ 4 day"); 
$day5=date('d', $add5); 
$add6 = strtotime("+ 5 day"); 
$day6=date('d', $add6); 
$add7 = strtotime("+ 6 day"); 
$day7=date('d', $add7); 

$month=date('m'); 
$year=date('Y'); 

$alltime=array("12:00PM", "12:30PM", "1:00PM","1:00PM","1:30PM","2:00PM","2:30PM","3:00PM","3:30PM","4:00PM","4:30PM","5:00PM","5:30PM","6:00PM","6:15PM","6:30PM","6:45PM","7:00PM","7:15PM","7:30PM","7:45PM","8:00PM","8:15PM","8:30PM","8:45PM","9:00PM","9:15PM","9:30PM","9:45PM"); 

$allday=array($day1,$day2,$day3,$day4,$day5,$day6,$day7); 

foreach($alltime as $time) 
{ 
    foreach($allday as $day) 
    { 
     $check="Select * From restaurant,reservation where restaurant.resid=reservation.resid and time='$time' and date='$year-$month-$day' and username='$username' and status='active'"; 
     $result=mysql_query($check);    
    } 
} 

我想創建基本的每週日曆與數組中的特定時間。下面的代碼工作沒有問題。唯一的問題是$ check和$ result保持相同的名稱。foreach循環,並得到不同的查詢和結果

之後,我正在用mysql_num_row($ result)代碼檢查並執行它。但沒有運氣...因爲$結果和$檢查應不同

我想他們:

$check1=sql 

$result1=$check1 

$check2=sql 

$result2=$check2 

... 

$check203=sql 

$result=$check203 

裏面的foreach格式,這樣我就可以把它和做什麼,我想它。

我用

for($i; $i<=203; $i++) 

的foreach結果是瘋狂的長,也沒有工作。

那麼,如何使用唯一編號或任何可以彼此分離的$ check和$ result來執行操作?

回答

2

這種方式非常慢,會做很多查詢而沒有結果。更好的方法是查詢所有預訂,然後在PHP中填寫沒有找到預留的空白處。

SELECT 
    time, date, id, name, etc 
FROM 
    restaurant 
    INNER 
    JOIN reservation 
     ON restaurant.resid=reservation.resid 
WHERE 
    date BETWEEN $start AND $end and username='$username' and status='active' 

$start = date('d-m-Y', strotime('midnight'))$end = date('d-m-Y', strtotime('+7 day, midnight'))

+0

+1呈現出更好的方法來解決問題 – bestprogrammerintheworld 2013-05-12 18:46:55