Make checkbox selected as database data!

Asked

Viewed 127 times

0

According to the code below, I need to make sure that when loading the page, a table is loaded with checkbox marked, according to database data, for example:

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

Where the X would be according 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 website, 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>";
  • When giving "echo" in the input, make a check to see if that should be checked, if that’s the case put the checked attribute="checked".

  • Because it is José, I did it, but I have not yet found the correct logic to mark only those that exist in the database, when I do this check, he marks some and then there are other checkboxes if you have any marked, I am trying here, anything I put the result, if you have another alternative I will be grateful for the help xD

2 answers

1

You can check whether the checkbox array exists within the data coming from the database with the 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>";
  • Dude, it didn’t work for me because of the two-dimensional arrays. in_array does not work in two-dimensional arrays (if I am not mistaken)

  • the in_array checks if an equal objective contains within the array, only if its array is different from the source/destination you cited in the example, I ran the test with your code and it worked right.

  • You’re right, I put the array X and Y this way to better be understood what I was talking about, however the array X and Y, would be a Array status, I will edit to be understood better.

0


Solution found:

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

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

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

After I print the checkbox case the variable $marcado be equal to true I print the checkbox with the property checked="checked" if not, print normal without 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 website, 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>";

Browser other questions tagged

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