foreach循环并获得不同的查询和结果

时间:2013-05-12 18:20:36

标签: php mysql arrays loops foreach

$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保持相同的名称。

之后我用mysql_num_row($ result)代码检查并执行它。但没有运气......因为$ result和$ check应该是不同的

我想得到它们:

$check1=sql

$result1=$check1

$check2=sql

$result2=$check2

...

$check203=sql

$result=$check203

在foreach中格式化,所以我可以接受它,并用它做任何我想做的事。

我用过

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

foreach结果很疯狂并且没有用。

那么如何使用唯一编号或任何我可以彼此分开的结果进行$ check和$ result?

1 个答案:

答案 0 :(得分: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'))

的位置