2014-02-11 52 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