Jump to content

foreach issue


gmc1103

Recommended Posts

Hello

I'm having problems regarding coding  right the following problem

One class (students) can have class all together or/and divided (haveDesdobramento) into 2 shifts

So in the beginning i'm choosing all class and defined 100 hours

Using a foreach because i need to check  holidays, school breaks, start and end of school

 foreach (range( 0, $datediff ) as $day) {
        $internal_date = date( INTERNAL_FORMAT, strtotime( "{$startDate} + {$day} days" ) );
        $this_day = date( INTERNAL_FORMAT, strtotime( $internal_date ) );
        $this_month = date( INTERNAL_FORMAT, strtotime( $internal_date ) );
        if($haveDesdobramento === "1") {
            if ((isSegunda( $internal_date )) && !isExcludedDate( $internal_date )) {
                $cronograma[$this_month][] = $this_day;
                if($temposSegunda != null) {
                    if($sum>=$horasAll){
                        $nhoras = $sum - $horasAll;
                        $temposSegunda = $temposSegunda - $nhoras;
                        echo "tSegunda: ".$temposSegunda;
                    }
                    if ($haveDesdobramento === "1" && $haveAllClass === null) {
                        if($totais<=$horasTurnos){
                            if ($sameDayShifts === "1") {
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = $temposSegunda;
                            }
                            else{
                                $days = floor($horas/$temposSegunda);
                                $hours_remaining = $horas - $days * $temposSegunda;
                                $cronograma[$this_month][] = $hours_remaining;
                            }
                        }

                    }
                    if ($haveDesdobramento === "1" && $haveAllClass === "1") {
                        if($sum<=$horasAll){
                            if ($sameDayShifts === "1") {
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = $temposSegunda;
                            }
                            else {
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = 0;
                                $total += $temposSegunda;
                                $cronograma[$this_month][] = $total;

                            }
                        }
                    }
                    else{
                        $days = floor($horas/$temposSegunda);
                        $hours_remaining = $horas - $days * $temposSegunda;
                        $cronograma[$this_month][] = $hours_remaining;
                    }
                }
                $sum += $temposSegunda;
            }
        }

 

It works but it stops at 102, not 100 like it should (choosing 3 hours each time) and the array returned gives me this

 

 

Any help?

 

Thanks

 

2021-06-07 - 3 - 0 - 99
2021-06-14 - 3 - 0 - 102
Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 661

2021-06-21 - - 0 - 102
Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 670

Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 661

2021-06-28 - - 0 - 102
Notice: Undefined offset: 1 in /home/esmaior/public_html/miga/db/crud/profissionais/response.php on line 670

Total: 102

 

 

 

Link to comment
Share on other sites

Hi

26 minutes ago, gw1500se said:

Add this before the 'foreach' to make sure it does what you expect.


echo "<pre>";
print_r(range( 0, $datediff ));
echo "</pre>";

The errors are likely related. Which lines are 661 and 670?

It does do..

The problem is inside the foreach loop, i have put some echos and this is really strange, if you look my code you willsee a lot of echos to help me to debug

 if($haveDesdobramento === "1") {
            if ((isSegunda( $internal_date )) && !isExcludedDate( $internal_date )) {
                $cronograma[$this_month][] = $this_day;
                if($temposSegunda != null) {
                    if ($haveDesdobramento === "1" && $haveAllClass === null) {
                        if($totais<=$horasTurnos){
                            $t = $sum + $temposSegunda;
                            if($t > $horasTurnos){
                                $n1 = abs($t - $horasTurnos);
                                $temposSegunda = $temposSegunda - $n1;
                            }
                            if ($sameDayShifts === "1") {
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = $temposSegunda;
                            }
                            else{
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = 0;
                                $total += $temposSegunda;
                                $cronograma[$this_month][] = $total;
                            }
                        }

                    }
                    if ($haveDesdobramento === "1" && $haveAllClass === "1") {
                        echo "1ª Soma:".$sum;
                        print "<br>";
                        if($sum<$horasAll){
                            echo "2ª Soma:".$sum;
                            print "<br>";
                            if(($sum+ $temposSegunda) > $horasAll){
                                echo "3ª Soma:".($sum+ $temposSegunda);
                                print "<br>";
                                $n1 = abs(($sum+ $temposSegunda) - $horasAll);
                                $temposSegunda = $temposSegunda - $n1;
                                print "<br>";
                            }
                            if ($sameDayShifts === "1") {
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = $temposSegunda;
                            }
                            else {
                                echo "aqui 2";
                                print "<br>";
                                $cronograma[$this_month][] = $temposSegunda;
                                $cronograma[$this_month][] = 0;
                                $total += $temposSegunda;
                                $cronograma[$this_month][] = $total;
                                $sum += $temposSegunda;

                            }
                        }
                    }
                    else{
                        echo "aqui";
                        $days = floor($horas/$temposSegunda);
                        $hours_remaining = $horas - $days * $temposSegunda;
                        $cronograma[$this_month][] = $hours_remaining;
                    }
                }
            }
        }

And with all those debugs i'm receiving this

1ª Soma:0
2ª Soma:0
aqui 2
1ª Soma:3
2ª Soma:3
aqui 2
1ª Soma:6
2ª Soma:6
aqui 2
1ª Soma:9
2ª Soma:9
aqui 2
1ª Soma:12
2ª Soma:12
aqui 2
1ª Soma:15
2ª Soma:15
aqui 2
1ª Soma:18
2ª Soma:18
aqui 2
1ª Soma:21
2ª Soma:21
aqui 2
1ª Soma:24
2ª Soma:24
aqui 2
1ª Soma:27
2ª Soma:27
aqui 2
1ª Soma:30
2ª Soma:30
aqui 2
1ª Soma:33
2ª Soma:33
aqui 2
1ª Soma:36
2ª Soma:36
aqui 2
1ª Soma:39
2ª Soma:39
aqui 2
1ª Soma:42
2ª Soma:42
aqui 2
1ª Soma:45
2ª Soma:45
aqui 2
1ª Soma:48
2ª Soma:48
aqui 2
1ª Soma:51
2ª Soma:51
aqui 2
1ª Soma:54
2ª Soma:54
aqui 2
1ª Soma:57
2ª Soma:57
aqui 2
1ª Soma:60
2ª Soma:60
aqui 2
1ª Soma:63
2ª Soma:63
aqui 2
1ª Soma:66
2ª Soma:66
aqui 2
1ª Soma:69
2ª Soma:69
aqui 2
1ª Soma:72
2ª Soma:72
aqui 2
1ª Soma:75
2ª Soma:75
aqui 2
1ª Soma:78
2ª Soma:78
aqui 2
1ª Soma:81
2ª Soma:81
aqui 2
1ª Soma:84
2ª Soma:84
aqui 2
1ª Soma:87
2ª Soma:87
aqui 2
1ª Soma:90
2ª Soma:90
aqui 2
1ª Soma:93
2ª Soma:93
aqui 2
1ª Soma:96
2ª Soma:96
aqui 2
1ª Soma:99
2ª Soma:99
3ª Soma:102

aqui 2
1ª Soma:100
1ª Soma:100

And the array is created i have this (last 3 lines)

2021-06-07 - 3 - 0 - 99
2021-06-14 - 1 - 0 - 100
2021-06-21 - 1 - 0 - 100

same in 2 differents dates but i have reached the 100 (hours) in first date (2021-06-14)

I don't understand why this last line

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.