我试图在下拉框一侧显示商店的状态,显示商店是打开还是关闭。下拉框中的状态是正确的,但下拉列表旁边的状态不正确。正确的一个值来自循环,我希望该值显示在循环外部。我正在使用的循环是foreach。请参阅附图以获得更好的理解。我得到了值但存储的最后一个值,我想要该行的第一个值。
我试图通过多种方式来解决这个问题,但到目前为止还没有运气。
我的代码是: -
<?php
date_default_timezone_set('Australia/Melbourne');
$datetime = new \DateTime();
$listItem = array('<li class="active">', '</li>');
$curDay = date('l');
$status = array( "Now Open" ,"Closed", "Opening Soon", "Closing Soon", "Open 24 hours", " ");
$times = array(
1 => array('day' => 'Monday', 'open' => $oMon, 'close' => $cMon),
2 => array('day' => 'Tuesday', 'open' => $oTue, 'close' => $cTue),
3 => array('day' => 'Wednesday', 'open' => $oWed, 'close' => $cWed),
4 => array('day' => 'Thursday', 'open' => $oThur, 'close' => $cThur),
5 => array('day' => 'Friday', 'open' => $oFri, 'close' => $cFri),
6 => array('day' => 'Saturday','open' => $oSat, 'close' => $cSat),
7 => array('day' => 'Sunday', 'open' => $oSun, 'close' => $cSun));
$html= "" ;
$html .= "<table class='table table-striped' border='0' align='center' cellpadding='5' cellspacing='15'>";
$i = 1;
$cd = $datetime->format('N');
$timenow = date("H:i:s", time());
// Create an array of day numbers that start with current day and loops around
//$day_order = array_merge(range($cd, 7), range(1, $cd-1));
$day_order = range($cd, 7);
if ($cd != 1) {
$day_order = array_merge($day_order, range(1, $cd-1));
}
foreach ($day_order as $daynum): {
$oc = $times[$daynum];
$openingTime = $oc['open'];
$closingTime = $oc['close'];
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
if ($cd == $daynum) {
if ($openingTime === null && $closingTime === null) {
$s = $status[4];
}
elseif ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}
elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}
elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
} else {
$s = $status[0];
}
} else {
$s = " ";
}
$html .= "<tr>";
$html .= "<td class='span2'>". $oc['day']."</td>";
// echo "<td> <span class='white-text' style='margin-right: 3em;'> </td>";
$html .= "<td>".$openingTime." ".$closingTime."</td>";
// echo "<td> <span class='white-text' style='margin-right: 3em;'> </td>";
$html .= "<td class='span2'>".$s."</td>";
$html .= "</tr>";
}
$datetime->add(new \DateInterval('P1D'));
endforeach;
$html .= "</table>";
echo $html;
?>
答案 0 :(得分:0)
Hey Theodore Messinezis,我已经解决了这个问题......它太简单了......
我只需添加此
$st = ""; // <- I added the declaration here
foreach ($day_order as $daynum): {
...
...
.
}
$st .= $s;// <- I add this line inside the loop to get value outside the array
endforeach;
echo $.st; <- Output