Select in php recognizes only one variable

Asked

Viewed 47 times

-1

I need to search the database for dates and compare them to the dates generated by a for. If they’re the same, they turn red.

I already got the numbers for one Select and store them in one array using another for, I make the comparison and it prints the values I passed to the array, but only one of the dates is in red.

I did the test and only the one with the highest ID value (referring to the dates that are searched in the database and stored in the array) is being compared and "painted" red. I already tested changing the ids (because the bank only has two dates to be compared) and the one that gets the biggest ID always turns red.

Follow the code below. I believe the problem may be in comparison mode or even in the way the database selects data.

for($i=0;$i<=$nr_dias;$i++){
    $query = "SELECT * FROM expedientes_diferenciados";
    if ($result = $conexao->query($query)) {
        /* fetch object array */
        $k=0;
        while ($obj = $result->fetch_object() and $k<2) {                           
                $diaind=$obj->data;
                $array1[$k]= date("d-m-Y", strtotime($diaind));
                $motivo[$k] =$obj->motivo;
                $k++;
    }}

    $dia = date("d-m-Y",mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
    $dia_sql = date("Y-m-d",mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
    $semana=$dias_semana[date("D",mktime(0,0,0,date("m"),date("d")+$i,date("Y")))];
    $lock_cafe="";$lock_almoco="";$lock_jantar="";

    //Condicionais
    $b=0;
    while($b<2){    
        if($semana=="Sábado" or $semana=="Domingo" or $dia==$array1[$b]) {
            $cor = "red";
            $msg_expediente = "Sem expediente<br>";
            echo $array1[$b];
        }
        else{
            $msg_expediente = "Expediente normal<br>";
            $cor = "white";
        }
        $b++;
    }
}
  • You have an idea, that you are resetting the variable $k=0 and $b=0 by the loop of your initial is.. you should declare are two variables outside the FOR loop. and you’re also not incrementing $k anywhere for it to get to the value 2. you should also put the comparisons between () within your while.. ex: while(($obj = $result->fetch_object())) AND ($a>2))

1 answer

0

$query = "SELECT * FROM expedientes_diferenciados";
if ($result = $conexao->query($query)) {
    /* fetch object array */
    $k=0;
    while ($obj = $result->fetch_object() and $k<2) {                           
            $diaind=$obj->data;
            $array1[$k]= date("d-m-Y", strtotime($diaind));
            $motivo[$k] =$obj->motivo;
            $k++;
}}

This block can be OUT of the first FOR, even for the sake of performance, as are 2 fields, does not affect much, more imagine with 200000 or more, it will recover every for loop loop loop, and is not dependent on the values of the loop

Inside the second while, there has to be a foreach, for it to go through the data of the array1, ai o if deve comparar esses valores, but in the second round of the while, the values of the back 1 are superimposed, ai sempre a saída desse segundo while é o do ultimo valor percorrido....

Browser other questions tagged

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