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;

Why is this not working? It is going wrong @ foreach($check as $date => $text) { but what do i have to change to let this work. I'm doing this So i only have to type the function once instead of copy pasting it everywhere.

2
  • You start with "And again", which is a strange way to start a question. Also, what is it supposed to do, what does it actually do, and why do the dates in Sign1 have no relationship to their descriptions? Commented Feb 11, 2014 at 10:30
  • Please add tags to your question. Especially about the language involved. Commented Feb 11, 2014 at 10:34

1 Answer 1

1

This is to do with variable scope. Dateoption cannot see the $check variable. The php documentation describes this as: 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.

You need to pass $check as a parameter to the Dateoption method.

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah we are in the right way now, but there is nothing showing up in the select sections? Do you know anything about that?
Sign1 and Sign2 also need to return the output of Dateoption. I have updated my answer.
Aaaahhh like that! Thnxs man! This helps me a lot! (Yeah i know i'm sometimes a little bit noobisch but thats why i ask and i learn a lot about it!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.