Make checkboxes selected, according to bank data!

0

As the code below, I need to make a table loaded with checked checkboxes, depending on the bank's data, for example:

                Destino
             _______________________________________
    Origem  |   1   |   2   |   3   |   4   |   5   |
    --------|-------|-------|-------|-------|-------|
       1    |       |   X   |       |       |       |
    --------|-------|-------|-------|-------|-------|
       2    |       |       |   X   |       |       |
    --------|-------|-------|-------|-------|-------|
       3    |       |       |       |   X   |       |
    --------|-------|-------|-------|-------|-------|
       4    |       |       |       |       |   X   |
    --------|-------|-------|-------|-------|-------|
       5    |       |       |       |       |       |

Where X would conform to the database, forming the following array :

   $transicao => array(
              (int) 0 => array(
                      'origem' => 1,
                      'destino' => 2
              )
              (int) 1 => array(
                      'origem' => 2,
                      'destino' => 3
              )
              (int) 2 => array(
                      'origem' => 3,
                      'destino' => 4
              )
              (int) 3 => array(
                      'origem' => 4,
                      'destino' => 5
              )

Current code:

Copying and pasting the code on this site , you will see the same in operation

<?php

   $status = array(
              [
                  'origem' => 1,
              ],
              [
                  'origem' => 2,
              ],
              [
                  'origem' => 3,
              ],
              [
                  'origem' => 4,
              ]  
   );

//simulando dados do banco de dados
   $transicao = array(
              [
                  'origem' => 1,
                  'destino' => 2
              ],
              [
                  'origem' => 2,
                  'destino' => 3
              ],
              [
                  'origem' => 3,
                  'destino' => 4
              ],
              [
                  'origem' => 4,
                  'destino' => 5
              ]  
   );

echo "
<table cellpadding='0' cellspacing='0'>
    <tr> <!-- Aqui é montado a primeira linha da tabela que é a do cabeçalho -->
        <th> ORIGEM </th>";
        foreach ($status as $xkey => $xvalue) : 
            echo "<th bgcolor='#cccc0'> " . $xvalue['origem'] . "</th>";
        endforeach;
        echo "</tr><tr>";
    //      foreach que percorre as colunas
            foreach ($status as $ykey => $yvalue) : 
                echo "<td bgcolor='#ddddd'> " . $yvalue['origem'] . "</td>";
                foreach ($status as $xkey => $xvalue) :
                    $value = $yvalue['origem'] . '|' . $xvalue['origem'];
                    echo "<td>
<input type='checkbox' name='origem" . $value . "destino value='" . $value . "'>
                    </td>";
                endforeach;
                echo "</tr>";
            endforeach; 
echo "</table>";
    
asked by anonymous 04.01.2017 / 18:08

2 answers

0

Solution found:

I created a $marcado variable within the foreach that runs through array $x to insert checkbox , initialize variable as false (thus: $marcado = false; ) and then did the following verification:

If $yvalue (source) is equal to $transicoes_value['origem'] (array source $transicoes ) and $xvalue is equal to $transicoes_value['destino'] (array destination $transicoes ), then I wrap the $marcado = true variable %.

if ($yvalue == $transicoes_value['origem']
 && $xvalue == $transicoes_value['destino']) {
    $marcado = true;
}

After I print the checkbox if the variable $marcado is equal to true I print the checkbox with the property checked="checked" if not, print normal without the property checked="checked" .

Thus:

if ($marcado == false) {
    echo "<td>
        <input type='checkbox' 
               name='origem" . $value . "destino value='" . $value . " 
               checked='checked'>
    </td>";
} else {
     echo "<td>
        <input type='checkbox' 
               name='origem" . $value . "destino value='" . $value . ">
    </td>";
}

Final code:

Copying and pasting the code on this site , you will see the same in operation

<?php

   $status = array(
              [
                  'origem' => 1,
              ],
              [
                  'origem' => 2,
              ],
              [
                  'origem' => 3,
              ],
              [
                  'origem' => 4,
              ],
              [
                  'origem' => 5,
              ] 
   );

//simulando dados do banco de dados
   $transicao = array(
              [
                  'origem' => 1,
                  'destino' => 2
              ],
              [
                  'origem' => 2,
                  'destino' => 3
              ],
              [
                  'origem' => 3,
                  'destino' => 4
              ],
              [
                  'origem' => 4,
                  'destino' => 5
              ]  
   );

echo "
<table cellpadding='0' cellspacing='0'>
    <tr> <!-- Aqui é montado a primeira linha da tabela que é a do cabeçalho -->
        <th> ORIGEM </th>";
        foreach ($status as $xkey => $xvalue) : 
            echo "<th bgcolor='#cccc0'> " . $xvalue['origem'] . "</th>";
        endforeach;
        echo "</tr><tr>";
    //      foreach que percorre as colunas
            foreach ($status as $ykey => $yvalue) : 
                echo "<td bgcolor='#ddddd'> " . $yvalue['origem'] . "</td>";
                foreach ($status as $xkey => $xvalue) :
                    $marcado = false;
                    $value = $yvalue['origem'] . '|' . $xvalue['origem'];
                    foreach($transicao as $transicao_key => $transicao_value) :
                    if ($yvalue['origem'] == $transicao_value['origem']
                     && $xvalue['origem'] == $transicao_value['destino']) {
                        $marcado = true;
                    }
                endforeach;
                if ($marcado == true) {
                    echo "<td>
      <input type='checkbox' 
             name='origem" . $value . "destino value='" . $value . "
             checked='checked'>
                    </td>";
                } else {
                    echo "<td>
      <input type='checkbox' 
             name='origem" . $value . "destino value='" . $value . " '>
                    </td>";
                }
                endforeach;
                echo "</tr>";
            endforeach; 
echo "</table>";
    
05.01.2017 / 12:02
1

You can check if the checkbox array exists within the data that is coming from the database with in_array

$checked = (in_array(['origem'=>$yvalue, 'destino'=>$xvalue], $transicao) ? 'checked="checked"' : '');

echo "<td>";
    echo "<input type='checkbox' name='origem" . $value . "destino value='" . $value . "' ".$checked.">";
echo "</td>";
    
04.01.2017 / 19:37