Return all results of a query with mysqli_fetch_array

Asked

Viewed 351 times

1

inserir a descrição da imagem aquiinserir a descrição da imagem aquiHello,

I am trying to implement a block scheduling system where the user chooses the date and location of his preference. After that, he will choose the times according to availability and need to block schedules that are unavailable. I tried to search the database and create an array of all schedules that are possible to schedule to compare with the results obtained, but I believe that I am with some logic error, because the function mysqli_fetch_array does not return all values and starts a new search when it falls in the if() condition I put. Exemplifying:

Have been scheduled the times of 18h00, 19h00 and 20h00 for the day 28/05/2018 on the court whose id = 1.

With the code below, it returns me 3 entire columns with the arrays specified, and in the first column only the time of 18h00 appears unavailable, in the second column only 19h00 and in the third column only 20h00 as unavailable.

Any idea to adjust and bring me just a column with the three times unavailable? Thank you so much already

                   $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;
                          }

                     }  
  • You’re a little confused, you can add an image of how the output is and how it should come out

  • 1

    Image added with the output of 3 searches I commented. It should only appear one of them with registered times in the bank appearing as "Unavailable". I thank you already.

  • Now it became clearer, this occurs because of the two loops, one inside the other, use the function in_array as in the answer

1 answer

1


One solution I found for your problem is the following:

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

$horas_cadastradas = mysqli_fetch_array($query);

You have the total hours matrix, which in the example I assigned as $horas_totais.

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

$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 achieve 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

Reference of the in_array function()

while of creation of array():

while ($dados_do_mysql = mysqli_fetch_array($query)){
    $horas_cadastradas[]=$dados_do_mysql['hora'];
}
  • @wess_Smith, Thank you for your reply, but I was not successful. Putting the code as you passed it worked, the problem is when I change the variable you assigned as $horas_cadastradas = array("18:00","19:00","20:00"); to receive the query from the database: $horas_cadastradas = mysqli_fetch_array($query). The output is a syntax error where it asks for an array and we are passing a string. I tried to do the conversion, but the output goes back to my initial problem.

  • then do a while and create the array within it

  • Good! worked perfectly. Thanks @Wess_smith

  • Thank you for being able to help

Browser other questions tagged

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