How to dribble the table tags that are inside the foreach in PHP and HTML

2

I need to set up a table of days and events. When one of the days of the event is the same as the table day, it has to appear on the screen in the correct place. I am already able to compare and show, however, because the table is all disassembled because of the algorithm that searches and runs, I can not make the table ever right!

Would anyone know a solution to get the table mounted by skipping these tags?

Follow the table code:

<table class="table table-bordered">
    <thead>
        <tr>
          <th><center>Turno \ Dia</center></th>
          <?php
            include_once 'classes/semestresclass.php';

            if(!isset($_SESSION)) {session_start();} 

            $idSemestre = $_SESSION['SemestreGeral'];

            $oSemestre = new semestresclass();
            $oSemestre -> listarEdicao($idSemestre);  

            $array = mysql_fetch_array($oSemestre->retorno());

            $start_date = $array['DataDeInicio'];
            $end_date = $array['DataDeTermino'];

            $inicio = new DateTime($start_date);
            $fim = new DateTime($end_date);
            $fim->modify('+1 day');

            $interval = new DateInterval('P1D');
            $periodo = new DatePeriod($inicio, $interval ,$fim);

            foreach($periodo as $data){
                echo '<th><p><center>'.$data->format("d/m/Y").'</p><p>'.$data->format("l").'</p></center></th>';

                include_once 'classes/bancasclass.php';

                if(!isset($_SESSION)) {session_start();}  

                $idSemestre = $_SESSION['SemestreGeral'];

                $oBanca = new bancasclass();
                $oBanca -> listar ($idSemestre);   

                while ($arrayBancas = mysql_fetch_array($oBanca->retorno())){
                    if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
                       echo '<td>teste</td>';
                    }
                } 
            }
          ?>
    </table>

In this scheme I also have schedules to organize in this table ... I'm trying to go in parts, first solve the day and then the schedule, but maybe I'm going the wrong way, maybe I have to do everything together .. In the case, the first column on the left will have the 'morning', 'late' and 'night' shifts and, depending on the time of the event, put it according to the two axes.

I'm trying with a table, but no table solutions are also welcome !!

Table:

    
asked by anonymous 09.11.2014 / 20:51

2 answers

5

You have some questions to address so you can solve your problem. The information below will not solve all your problem, but aims to create the basis for you to deal with it:

Opening and closing tags

You have to pay attention to opening and closing the tags because they have to be closed in the reverse order they were opened:

echo '
<th>
  <p>
    <center>'.$data->format("d/m/Y").'</p>
    <p>'.$data->format("l").'</p>
  </center>
</th>';

Should be:

echo '
<th>
  <center>
    <p>'.$data->format("d/m/Y").'</p>
    <p>'.$data->format("l").'</p>
  </center>
</th>';

The best way is to always have the indented code because it makes it easier to read the code, and you can see problems faster than this one.

Or <td> with text or <td> empty, but always <td>

The markup of <table> has to be present in its entirety except for some exceptions where we use the colspan or rowspan . For this reason, if you have a check that will or will not generate a <td> , you'll need to have a fallback for a <td> empty so you do not break the layout table:

if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
    echo '<td>teste</td>';
}

Should be:

if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
    echo '<td>teste</td>';
}
else{
    echo '<td></td>';
}

In this way, if a match exists on the date, it will be sent to the browser with <td>teste</td> , otherwise, an empty co% will be sent, thus avoiding breaking the layout of the table.

Output is per line and not per column

Everything you have inside this cycle is working as if the output to the browser was per column, but actually the output is per line because the browser reads the code from top to bottom and not from left to right:

foreach ($periodo as $data) {
  // dados devem ser trabalhados por "row"
}

Similarly, within the cycle above, you are:

  • Consecutively include <td> ;
  • Concurrently calling the function bancasclass.php ;

I do not know why this happens, but what you expect is session_start() . only once at the beginning of your file, unless for some reason you are destroying the session within session_start() . Likewise, the bancasclass.php file should be called once and the code present in it is used within the loop if necessary. Since the file is named bancasclass.php , it is assumed that you have a ...class.php . p>

Finally, your cycle class is sending while() to the but the <td> must be within <td> . Analyzing together with the image you put, you are supposed to have 7 (seven) <tr> within each <td> , being the first one with the text "Morning", "Afternoon", or "Night" ...

Given all this, and the fact that we do not know the content of your <tr> file, it is assumed that it processes data that depends directly on your cycle bancasclass.php , I suggest the following adaptation to the code:

// Para guardar o HTML do cabeçalho da tabela
$cabecalhosHtml = '';

// Para guardar o HTML das linhas do corpo da tabela
$linhasHtml = '';

foreach ($periodo as $data) {

  /* Processar cabeçalho da coluna
   */
  $cabecalhosHtml.= '
  <th style="text-align:center;">
    '.$data->format("d/m/Y").'
    <br>
    '.$data->format("l").'
  </th>';

  /* Processar celulas da coluna
   */
  include_once 'classes/bancasclass.php';

  if (!isset($_SESSION)) { session_start(); }

  $idSemestre = $_SESSION['SemestreGeral'];

  $oBanca = new bancasclass();
  $oBanca -> listar ($idSemestre);   

  // abrir linha da tabela
  $linhasHtml.= '<tr>';

  // processar celunas da linha
  while ($arrayBancas = mysql_fetch_array($oBanca->retorno())) {

    if (date('Y-m-d', strtotime($arrayBancas['dataHora'])) == $data->format('Y-m-d')) {
      $linhasHtml.= '<td>teste</td>';
    }
    else {
      $linhasHtml.= '<td></td>';
    }
  }

  // fechar linha da tabela
  $linhasHtml.= '</tr>';
}

echo $cabecalhosHtml;
echo $linhasHtml;
    
11.11.2014 / 12:41
2

You need to be aware of all the table's tags when you print the values. From what I've seen you do not have the tags correctly, inspect the generated HTML in the browser , and see the result. You will check that you are missing tag's and so the table is not right.

Each row of the table must start with <tr> and end with </tr> , ie when you have to print rows in a for cycle you must print those tags whenever you change lines.

Here's a basic example of how you can have a table:

<table border="1" style="width:300px">
    <thead>   <!-- Inicio do cabeçalho -->
        <tr>    <!-- Inicio de linha     -->
            <th> 
                Nome
            </th>
            <th>
                Sobrenome 
            </th>
            <th>
                Telefone
            </th>
        </tr>   <!-- fechar linha de cabeçalho -->
    </thead>    <!-- fechar cabeçalho          -->
    <tbody>     <!-- inicio do corpo da tabela -->
        <tr>    <!-- Inicio de linha           -->
            <td>
                João
            </td>
            <td>
                Silva
            </td>
            <td>
                912345678
            </td>
        </tr>      <!-- Fim de linha -->
        <tr>
            <td>
                Juliana
            </td>
            <td>
                Sobral
            </td>
            <td>
                912345677
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>
                Total
            </td>
            <td>
                Total
            </td>
            <td>
                Total
            </td>
        </tr>
    </tfoot>
</table>
    
11.11.2014 / 10:34