Return all results of a query with mysqli_fetch_array

1

Hello,

I'mtryingtoimplementablockschedulingsystemwheretheuserchoosesthedateandlocationoftheirchoice.Afterthat,hewillchoosethetimesaccordingtoavailabilityandneedtoblockschedulesthatareunavailable.Itriedtosearchthedatabaseandcreateanarrayofallpossibleschedulingtimestocomparewiththeresultsobtained,butIbelieveIamwithsomelogicerror,becausethemysqli_fetch_arrayfunctiondoesnotreturnallvaluesandstartsanewonesearchwhenitfallsintheconditionofif()thatIput.Exemplifying:

Thescheduleshavebeenscheduledfrom6:00p.m.,7:00p.m.and8:00p.m.on05/28/2018intheblockwhoseid=1.

Withthecodebelow,itreturnsme3integercolumnswiththespecifiedarrays,beingthatinthefirstcolumnonlyappearstheunavailablethetimeof18h00,inthesecondcolumnonly19h00andinthethirdcolumnonly20h00asunavailable.>

Anyideatoadjustandbringmejustonecolumnwiththethreetimesunavailable?Manythanksfromnow

$consulta_sql="SELECT hora_agendada FROM tabela_agendamentos WHERE data='$data_escolhida_pelo_usuario' AND id_quadra_escolhida_pelo_usuario='1'";
                   $query   = mysqli_query($infos_conexao, $consulta_sql)

                    while ($dados_do_mysql = mysqli_fetch_array($query)){ 
                       foreach ( array("9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00") as $hora_array ) {
                            if($hora_array == $dados_do_mysql['hora']){
                                $hora_array ='Horário Indisponível';
                              }
                              echo $hora_array;
                          }

                     }  
    
asked by anonymous 08.05.2018 / 02:18

1 answer

1

One solution I found to your problem is this:

You have array() of hours that have already been chosen as the query response through mysqli_fetch_array() , which in the example I set as $horas_cadastradas , in your case would be:

$horas_cadastradas = mysqli_fetch_array($query);

You have the total hours matrix, which in the example I set to $horas_totais .

Using the foreach() loop in the $horas_totais array and using the in_array() function to check if the time exists in the $horas_cadastradas array:

$horas_cadastradas = array("18:00","19:00","20:00");
$horas_totais = array("9:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00");
foreach ($horas_totais as $hora) {
    if(in_array($hora,$horas_cadastradas)){
        echo '"Horário Indisponível"<br>';
    }else{
        echo $hora."<br>";
    }
}

We were able to get to the result of:

9:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
"Horário Indisponível"
"Horário Indisponível"
"Horário Indisponível"
21:00
22:00
23:00
  

Function reference in_array ()

while creation array() :

while ($dados_do_mysql = mysqli_fetch_array($query)){
    $horas_cadastradas[]=$dados_do_mysql['hora'];
}
    
08.05.2018 / 11:38