2016-09-08 1 views
0

ストアのステータスを、ストアが開いているか閉じているかを示すドロップダウンボックスの横に表示しようとしています。ドロップダウンボックス内のステータスは正しいですが、ドロップダウンボックスの横のステータスは正しくありません。正しい1つの値はループから来て、その値をループの外側に表示します。私が使用しているループはforeachです。より良い理解を得るために添付の画像をご覧ください。私は値を取得するが、最後の値が格納されている、私はその行の最初の値が欲しい。PHP - ループ外の単一の値を表示する(Laravel F/W)

私はこれを解決するために多くの方法にアプローチしましたが、これまでのところ運がありません。

enter image description here

マイコードは次のとおりです。 -

<?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; 






    ?> 
+3

あなたは本当にlaravelを使用していますか? – theomessin

+0

私はこのコードをlaravelで使用しています。私もPHPリセット、次の構文、それ以前の構文を使用してみましたが、 –

+0

私はあなたの質問が非常に広く不明であると信じています。ループの外側に単一の値を表示することは、あなたの下着の色を推測するように求めることと同じです。さらに、コードの短い断片、機能していない部分だけを提供する必要があります。 最終的なアドバイス:あなたは本当にあなたのコードの優雅さに取り組む必要があります。あなたのコードは、正確で、自明で、よく、美しくなければなりません。提供されるコードは上記のものではありません。 – theomessin

答えて

0

ねえセオドア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 
+0

Theodore Messinezis、私は良いコーダーではなく、それらを効率的に書く能力もありませんが、私は学びます。 –

関連する問題