Toggle Variable within While

Asked

Viewed 45 times

0

My system controls vehicle travel. When the customer inserts a particular trip to a line, this line has two vehicles associated with it. Then he takes the period, for example from 04/02/2019 to 08/02/2019, takes these two vehicles and inserts the trips in the travel table, through the while. Only I need to alternate the vehicles, each day it will insert a different vehicle. I wanted to try to do this without having to consult the travel chart at each one insert within the while.

public function store_teste(Request $request)
{
    $inicio = implode("-", array_reverse(explode("/", $request->input('inicio'))));
    $fim = implode("-", array_reverse(explode("/", $request->input('fim'))));
    $veiculo1 = $request->input('veiculo1');
    $veiculo2 = $request->input('veiculo2');

    while (strtotime($inicio) <= strtotime($dtfim2)) {           

        //ROTINA DO INSERT            
        //Na primeira passagem pelo while vai inserir o veículo1
        //Na segunda passagem pelo while vai inserir o veículo2
        //Na terceira pasagem pelo while vai inserir o veículo1
        //Na quarta pasagem pelo while vai inserir o veículo2

    $inicio = date ("Y-m-d", strtotime("+1 day", strtotime($inicio))); 
    }


}

There is no fixed day of the month or week, the first registration inserted will the vehicle 1, the second goes the 2. And then it alternates depending on the amount of days.

Can someone help me with this logic?

1 answer

2


All the examples here are on IDEONE.

There are several ways. See some:


Alternating with subtraction

$alterna = 1;
while ( ... ) {           
   $alterna = 1 - $alterna; // Ou vai ser 1-0, ou 1-1 sempre
   if ($alterna) {
      // var1
   } else {
      // var2
   }

Applied to your case:

$alterna = 1;
while ( ... ) {           
   $alterna = 1 - $alterna; // se for true vira false, se for false vira true
   $veiculo1 = $request->input($alterna?'veiculo1':'veiculo2');
}


Alternating with logical negation

$alterna = true;
while ( ... ) {           
   $alterna = !$alterna;
   $veiculo1 = $request->input($alterna?'veiculo1':'veiculo2');
}

This line here:

$alterna = !$alterna;

is equivalent to other constructions (some kind of crazy) that you find on the web:

$alterna = ($alterna == false);
$alterna = $alterna ? false : true; // essas são de doer, mas já vi piores


Alternating with bit inversion

$alterna = 0;
while ( ... ) {           
   $alterna = ~$alterna;
   $veiculo1 = $request->input($alterna?'veiculo1':'veiculo2');
}

Or by reversing the first bit:

$alterna = 0;
while ( ... ) {           
   $alterna ^= 1;
   $veiculo1 = $request->input($alterna?'veiculo1':'veiculo2');
}


In the examples above we are creating a situation where we only have two possible states. There are many other ways.


Switching with rest operator

If it were in a loop with an incremental counter of 1 in 1 (like most loops):

if ($contador % 2) {
   // var1
} else {
   // var2
}

in your case:

$veiculo1 = $request->input($contador%2?'veiculo1':'veiculo2');

This works because the operator of rest % will always be zero or one in a sequential counter (working with integers).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.