2014-02-11 66 views
0
function Sign1(){ 
    $check = array(
     '23-03-2014' => 'saturday 22 may', 
     '17-05-2014' => 'friday 16 may' 
    ); 
    Dateoption(); 
} 
function Sign2(){ 
    $check = array(
     '10-02-2014' => 'monday 10 feb', 
     '15-02-2014' => 'friday 15 feb', 
     '14-03-2014' => 'friday 14 march' 
    ); 
    Dateoption(); 
} 
function Dateoption(){ 
    $now = time(); 
    $result = array(); 
    foreach($check as $date => $text) { 
     if($now <= strtotime($date)) { 
      $result[] = $text; 
     } 
    } 
    $html = ''; 
    foreach($result as $v) { 
     $html .= '<option>'.$v.'</option>'; 
    } 
    return $html; 
} 
$Content= ' 
<div class="content"> 
    I am signing up for the following date:<br /> 
    <select name="date[0]"> 
     '. Sign1() .' 
    </select> 
    <select> 
     '. Sign2() .' 
    </select> 
</div> 
'; 
echo $Content; 

這是爲什麼不工作?這是錯誤的@ foreach($檢查爲$日期=> $文本){但我必須改變,讓這項工作。我這樣做所以我只需要鍵入一次函數,而不是複製粘貼到任何地方。功能的函數錯誤

+0

您從「又一次」開始,這是一個奇怪的開始問題的方式。此外,它應該做什麼,它實際上做了什麼,以及爲什麼Sign1中的日期與它們的描述沒有關係? –

+0

請爲您的問題添加標籤。特別是關於涉及的語言。 – arkascha

回答

1

這是關於可變範圍。 Dateoption無法看到$ check變量。 php documentation描述爲:However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.

您需要將$ check作爲參數傳遞給Dateoption方法。

function Sign1(){ 
    $check = array(
     '23-03-2014' => 'saturday 22 may', 
     '17-05-2014' => 'friday 16 may' 
    ); 
    return Dateoption($check); 
} 
function Sign2(){ 
    $check = array(
     '10-02-2014' => 'monday 10 feb', 
     '15-02-2014' => 'friday 15 feb', 
     '14-03-2014' => 'friday 14 march' 
    ); 
    return Dateoption($check); 
} 
function Dateoption($check){ 
    $now = time(); 
    $result = array(); 
    foreach($check as $date => $text) { 
     if($now <= strtotime($date)) { 
      $result[] = $text; 
     } 
    } 
    $html = ''; 
    foreach($result as $v) { 
     $html .= '<option>'.$v.'</option>'; 
    } 
    return $html; 
} 
$Content= ' 
<div class="content"> 
    I am signing up for the following date:<br /> 
    <select name="date[0]"> 
     '. Sign1() .' 
    </select> 
    <select> 
     '. Sign2() .' 
    </select> 
</div> 
'; 
echo $Content; 
+0

是的,我們現在正確的方式,但在選擇部分沒有任何東西顯示出來?你知道這件事嗎? – Harryaars

+0

Sign1和Sign2也需要返回Dateoption的輸出。我已經更新了我的答案。 –

+0

Aaaahhh那樣! Thnxs男人!這對我很有幫助! (是的,我知道我有時候會有點小氣鬼,但是這就是爲什麼我問我,並且我學到了很多東西!) – Harryaars