PHP foreach Loop和mysql Fetch关联数组问题(已发布值)



我只需要从数据库检查,如果天被阻止显示在一个选择框中禁用,如果不显示未来2个月的所有天在同一选择框。我得到关闭(封锁)的日子没有问题,但当我试图得到其余的日子,我得到的一切加倍。If NOT表示数组不工作。结果显示如下所示:

3月30日星期四3月30日星期四甚至从第一个循环开始的DISABLED day在循环后显示double。
进一步澄清:我正在使用For循环,以获得未来2个月的日期。我使用foreach循环来获取关闭的天数(星期日,星期一)的数组…所以我想在选择框中显示(日期与星期日或星期一禁用),并显示其余的天正常。

sql我

CREATE TABLE `check_availability` (
`id` int(10) NOT NULL,
`Day` varchar(10) NOT NULL,
`Open_hour` varchar(10) NOT NULL,
`Closed_hour` varchar(10) NOT NULL,
`ClosedDays` varchar(10) NOT NULL,
`Blocked` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `check_availability` (`id`, `Day`, `Open_hour`, `Closed_hour`, `ClosedDays`, `Blocked`) VALUES
(1, 'Sunday', '09:00am', '10:00pm', '', 1),
(2, 'Monday', '09:00am', '10:00pm', '', 1);

mysql取回代码。阵列正确。Array ([0] =>Array ([Day] =>[1] =>Array ([Day] =>星期一)通过禁用天数循环得到2个月内正确的周日或周一的所有日期。

$sqlClosedDays   = "SELECT Day FROM check_availability WHERE Blocked ='1'";             
$resultClosed    = mysqli_query($con, $sqlClosedDays);
$DaysClosed   = mysqli_fetch_all($resultClosed,MYSQLI_ASSOC);

我的for和Foreach循环,导致日期显示双。

echo "<select name='add_r_date' id='add_r_date'>";
for($i=0;$i<=60;$i++){
//Get the Closed Days   

//Display the Days according to limit   
$day=date('l,d M',strtotime("+$i day"));
$InsDay = date('Y-m-d',strtotime("+$i day"));   

foreach ($DaysClosed as $rowDaysClosed) {   
$dayClosed = $rowDaysClosed['Day'];
$dayClosedTrim= substr($dayClosed, 0, 3);
$dayTrim = substr($day, 0, 3);

//If not Closed set as available
if ($dayTrim === $dayClosedTrim ) { 
echo "<option disabled value='$InsDay'>$day</option> "; 
//$clDays[] = $day;     
} else {
echo "<option name='add_r_date' id='add_r_date' value='$InsDay'>$day</option>";
}
}//EOF FOREACH LOOP 
}//EOF FOR LOOP 
echo "</select>";

I try unset($day);或未设置($ dayClosed);

I try Break;在循环中,但问题仍然存在。

问题在于数值显示了2次。我只是想在disabled中显示休息日(周日)和休息日(周一),并显示休息日正常,以便人们可以选择可用的日期进行预订。

由于@CBroe建议在循环前和循环内添加布尔标志,问题得到了解决。在我用那些旗子检查之后。我还添加了break来停止循环。最后,我在代码的开头添加了对数据库的检查,如果存在任何关闭日期。如果不是,则根据for循环输出所有日期。下面是工作代码:

//SELECT START
echo "<select name='add_r_date' id='add_r_date'>";
for($i=0;$i<=30;$i++){
//Get the Closed Days 
//Display the Days according to limit   
$day=date('l,d M',strtotime("+$i day"));
$Nday = date('l',strtotime("+$i day"));
$NdayTrim= substr($Nday, 0, 3);
$InsDay = date('Y-m-d',strtotime("+$i day")); 
$counter = 0;
//CHECK IF THERE ARE ANY CLOSED/BLOCKED DAYS
if(count($DaysClosed ) != 0 ){
$clDays[] ='';  
$isOpen = 'true';
foreach ($DaysClosed as $rowDaysClosed) {   
$dayClosed = $rowDaysClosed['Day'];
$dayClosedTrim= substr($dayClosed, 0, 3);
$dayTrim = substr($day, 0, 3);

//If not Closed set as available
if ($dayTrim === $dayClosedTrim ) { 
$isOpen = 'false';
echo "<option disabled value='$InsDay'>$day</option> ";     
$clDays[] = $day;   
} 
$counter++;         
}//EOF FOREACH LOOP 

$total_count=$counter;

foreach ($clDays as $rowclDays) {
$daycl = $rowclDays;

if($daycl === $day && $total_count > 0) 
{
//    echo "<option disabled value='$InsDay'>$day</option> ";
$isOpen = 'false';
break;
}
else if($isOpen == 'true') {    
echo "<option name='add_r_date' id='add_r_date' value='$InsDay'>$day</option>";
break;
} 
}//EOF 2nd FOREACH 
//IF there arent any closing days
} else {    
echo "<option name='add_r_date' id='add_r_date' value='$InsDay'>$day</option>";

}       
}//EOF for loop     
echo "</select>";

您可以使用DISTINCT方法:

$sqlClosedDays   = "SELECT DISTINCT Day FROM check_availability WHERE Blocked ='1'"; 

或者你可以使用GROUP BY方法:

$sqlClosedDays   = "SELECT Day FROM check_availability WHERE Blocked ='1' GROUP BY Day";     

相关内容

  • 没有找到相关文章

最新更新