Traversing rows from a table within loops

0

What I want to do is to mark the times and dates in the front-end according to the records of a table, but with the code I have, I can only mark the first row of the table. How can I go through the whole table, marking all the corresponding dates?

I have had a certain success with another while instead of mysqli_fetch_assoc() using mysqli_fetch_array() however it repeats the number of rows in the table, multiplying the times of the front-end . Here is my code:

<div class="row">
   <?
   for ($i=0; $i < 12; $i++) { 
      $justDay=date('Y-m-d',strtotime("+$i days"));
      $days=formatDt(date('Y-m-d',strtotime("+$i days")));
      $dweek= strftime('%A', strtotime("+$i days, today"));
   ?>
   <div class="col-md-1 bord-head-table text-center">
      <span class="text-primary"><?= $days;?><br><!--linha exbe semana--></span><br>
      <span class="text-white fontweek"><?=utf8_encode($dweek); ?></span>
      <hr class="border-lime">
      <!-- chama tabela horas-->
      <? $sql = "SELECT * from horas";
      $qr=mysqli_query($conexao,$sql);
      $it=0;
      while ($ft=mysqli_fetch_array($qr)) { 
         $it++;
         $sqlAula = "SELECT * from monta_aula";
         $qrAula=mysqli_query($conexao,$sqlAula);
         $ftAula=mysqli_fetch_assoc($qrAula);
         $hrAula=$ftAula['hora_inicio_aula'];

         /* compara hora e data da tabela aula com a hora e data do front end e contorna o horário correspondente ao cadastrado no banco, na tabela aula*/
         if ($hrAula == $ft['hora'] and $justDay == $ftAula['dia_aula']) {
            $hc="border-class";
         } else{
            $hc="text-green-d";
         }
      ?>
      <div class="bord-hours">
         <a href="#" class="<?=$hc;?>">
         <?=$ft['hora'];?>
         </a><br>
      </div>
      <?    }//while ?>
   </div>
   <!--col md 1 - representa cada coluna, com data, dia da semana e horários (de 08:00:00 às 21:00:00)-->
   <? }//for ?>
</div>
<!--row-->

Table to go to:

Hourstable:

FrontEnd:

    
asked by anonymous 01.12.2017 / 01:38

1 answer

1

I believe you have to create some relationship between your for() and the queries that are being made later.

Every loop of for() you search all times on every day registered. Would not you just have to bring up records for that specific day that is calculated as% with%?

Another detail is that when you do $justDay = date('Y-m-d', strtotime('+$i days')) , you are only getting the first record of the query that can return n records.

You can try to do this as follows:

$sqlAula = "SELECT id_monta_aula FROM monta_aula WHERE dia_aula = '" . $justDay . "' AND hora_inicio_aula = '" . $ft['hora'] . "' LIMIT 1";

In this way, every time $hrAula = $ftAula['hora_inicio_aula'] is executed, the query will fetch a class (observe while() ) on the day calculated there in LIMIT 1 and in every query time $justDay for that day.

So you just need to check if there is any class in this query:

if (mysqli_num_rows($sqlAula) == 1)
{
    $hc = 'border-class'; // Tem aula nesse dia e horário!
}
else
{
    $hc = 'text-green-d'; // Não tem aula...
}

I do not really recommend using "SELECT * from horas" in queries, so I removed it, but then you can put whatever fields you need.

Remembering that in terms of performance, it is also not very recommended that you bring in the hours of the database as it is being done. However, I believe it will work normally.

The reason is very simple, * is repeated 12 times, that is, each time it is repeated, it will execute 1 query bringing schedules and more 14 below (one for each table time). This will all be equivalent to for() queries.

I hope I have helped! :)

    
04.12.2017 / 14:15