0

I'm trying to pass variables from a foreach function that fetch a mysql query to an array, to another function.

I declare the array before the foreach to make it global but no data are passed to the function. here is my code. If you have any idea of what i'm doing, thanks by advance for your help.

Of course if I replace the called function by what's inside it, everything works like a charm. but as I'll have to use it several times I would prefer to set my variables in a function.

function fillStuInfos() {
        global $studFName, $studLName,c$dateStart, $dateEnd;
        $studFName = $EventsGt['fName'];
        $studLName = $EventsGt['lName'];
        $dateStart = $EventsGt['startDate'];
        $dateEnd = $EventsGt['endDate'];
}

$email = $_POST['email'];

$EventsGt = array();

$getEventsQry = 'SELECT * FROM Companies WHERE Email_Company = "'.$email.'" ORDER BY startDate';
        foreach ($bddPDO->query($getEventsQry) as $EventsGt) {
                $intCur =  date_diff(date_create($today), date_create($EventsGt['endDate']));
                $intFut =  date_diff(date_create($today), date_create($EventsGt['startDate']));
                echo intval($intCur->format('%r%a'));
                if (intval($intCur->format('%r%a')) <= 4 && intval($intCur->format('%r%a')) > 0 ) {
                        fillStuInfos();
                } else if ( intval($intFut->format('%r%a')) <= 4 && intval($intFut->format('%r%a')) > 0 ){
                        fillStuInfos();
                } else {
                        echo 'No Datas!';
                        exit();
                }
        }

echo $studLName;

2 Answers 2

4

Forget that exists global and pass params to function.

function fillStuInfos($data) {
        $studFName = $data['fName'];
        $studLName = $data['lName'];
        $dateStart = $data['startDate'];
        $dateEnd = $data['endDate'];
}
...
foreach (...) {
    if (intval($intCur->format('%r%a')) <= 4 && intval($intCur->format('%r%a')) > 0 ) {
        fillStuInfos($EventsGt);
    } else if ( intval($intFut->format('%r%a')) <= 4 && intval($intFut->format('%r%a')) > 0 ){
        fillStuInfos($EventsGt);
    } else {
        echo 'No Datas!';
        exit();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

So evident, sometimes I forgot essentials !! Thanks
1

Your variable you want to be global is $EventsGt, not $studFName, $studLName, $dateStart, $dateEnd

With that said, I would recommend passing the event array as a parameter or passing the individual values as parameters instead.

1 Comment

So evident, sometimes I forgot essentials !! Thanks

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.