ik looks like inside foreach my code goes into if and else statement in each loop

Viewed 23

I am making a request to my db when I get event of the last 7 days. Then I'd like to check if for some days of this last week, I did not had entries so I can but a good array of data with ' ' if no input and the correct data if data were entered.

Let's say I have the seven last days :

[27,26,25,24,23,22,21]; 

Then if my date in db is not equal to one of the date in these array, in my array result I will put ' '; So in my example , I do not have entry for 24 and 23 so I'd like to have :

[data, data, data,'','',data,data]; 

So I have tried this code :

while($matinal = mysqli_fetch_object($get_matinal)) {

    foreach($lab as $key => $l):

            if (convertDateYmd($matinal->created_at) === $l ) {

                $matinal_result[$key] = $matinal->moyenne_fatigue;

            } else{

            }
    endforeach;
}

So basically $get_matinal is my query and got 5 values (5 entries in last week).

$lab is an array with 7 dates : [21,22,23,24,25,26,27];

So what I am trying to do is each time I read the first value of my query => $get_matinal, I am running a foreach loop to check if the date of this lines is present in my $lab array. If yes I'd like to keep the value if not, I'd like to have an empty string if the result.

But with my code, it looks like I enter everytime in the else... of I delete the else, I still have 5 true answer.

So my array $matinal_result should be = [data,data,data,'','',data,data] or I get ['','','','','','',''] ...

0 Answers
Related