PHP foreach循环和Mysqli Fetch关联数组问题(重复值)

iqjalb3h  于 2023-04-04  发布在  PHP
关注(0)|答案(2)|浏览(86)

我只需要从数据库中检查,如果一天被封锁,在一个选择框中显示为禁用,如果不显示未来2个月在同一个选择框中的所有日子。我得到关闭(封锁)天没有问题,但当我试图得到其余的日子,我得到的一切都加倍。如果没有其他数组不工作。结果显示如下所示的加倍:
3月30日星期四,3月30日星期四,甚至第一个循环中的禁用日在循环后显示双重。
进一步澄清:我使用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);

Mysqli获取代码。数组是正确的。Array([0] =〉Array([Day] =〉Sunday)[1] =〉Array([Day] =〉Monday))在循环通过禁用的日子得到我所有的日期是星期日或星期一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>";

我尝试unset($day);$dayClosed();
我试着打破;但问题仍然存在。
问题是,该值显示2次。我只是想显示在禁用关闭的日子(星期日)和(星期一),并显示休息日正常,以便人们可以选择可用的日期进行预订。

inn6fuwd

inn6fuwd1#

感谢@CBroe的建议,在循环之前和循环内部添加布尔标志,问题得到了解决。之后我使用这些标志进行检查。我还添加了Breaks来停止循环。最后,我在代码的开头添加了一个检查数据库是否存在任何关闭日期。如果没有,则根据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>";
tp5buhyn

tp5buhyn2#

您可以使用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";

相关问题