Skip to content
Advertisement

PHP – Check checkboxes if id is in database, checks first but not next ones?

I need to check the checkboxes that id is equal to id from user on database, Like:
User with ID 1
Permissions table: 1,2,3,4,5

User 1 has permissions of 1,3 and 4 USER PERM.
1 – 1
1 – 3
1 – 4

With the following code it checks the first permission but not the following ones

<?php
   foreach($col2 as $key => $value) {
      foreach($modulosPermissaoFuncio as $checkedModulo){
         if ($value["id"]==$checkedModulo["id_modulo"]){
            $checked1 = "checked";
         }else if($value["id"]<>$checkedModulo["id_modulo"]){
            $checked1 = "";
         }
      }

      echo ' <input type="checkbox" id="'.$value["id"].'" name="checkboxPermissoes[]" value="'.$value["id"].'" '.$checked1.'> &nbsp
      <label for="'.$value["id"].'">'.utf8_decode ( $value["descricao_site"]).'</label><br>';
   
   }
?>

This is Col1 and Col2:

enter image description here

This is user Permissions:

enter image description here

VAR_EXPORT:

$Col2:

array ( 
  0 => array ( 'id' => '11', 'descricao' => 'documentos', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Documentos', ), 
  1 => array ( 'id' => '14', 'descricao' => 'galerias', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Galerias', ), 
  2 => array ( 'id' => '15', 'descricao' => 'registos', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Registos', ), 
  3 => array ( 'id' => '16', 'descricao' => 'correspondencia', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Correspondência', ), 
  4 => array ( 'id' => '17', 'descricao' => 'portaria', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Portaria', ), 
  5 => array ( 'id' => '18', 'descricao' => 'refeicoes', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Refeições', ), 
  6 => array ( 'id' => '19', 'descricao' => 'imghome', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Imghome', ), 
  7 => array ( 'id' => '20', 'descricao' => 'imobilizado', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'Imobilizado', ), 
  8 => array ( 'id' => '21', 'descricao' => 'rh', 'id_empresa' => '1', 'estado' => '1', 'descricao_site' => 'RH', ),
)

$ModulosPermissaoFuncio:

array ( 
  0 => array ( 'id' => '73', 'uid' => '99999', 'id_modulo' => '18', 'id_empresa' => '1', ), 
  1 => array ( 'id' => '74', 'uid' => '99999', 'id_modulo' => '17', 'id_empresa' => '1', ), 
)

Advertisement

Answer

The reason it doesn’t work is because you’re not breaking the loop once you’ve found the matching entry in $modulosPermissaoFuncio.

e.g. in the case of the item 'id' => '18' from $col2, the inner foreach loop will find the entry 'id_modulo' => '18' in $ModulosPermissaoFuncio the first time it loops, and sets $checked1 to checked. So far so good.

The problem is, it doesn’t stop there. It loops again, but this time $value["id"] doesn’t match $checkedModulo["id_modulo"] (the latter is 17 this time), so it goes into the else block and sets $checked1 back to empty. Therefore, by the time you come to output the checkbox, $checked1 has been reset to empty.

So that’s what I meant in the comments where I said it’s overwriting the value, and you need to break out of the loop once you’ve found the value you want.

This should fix it:

if ($value["id"] == $checkedModulo["id_modulo"]){
    $checked1 = "checked";
    break;
}

Working demo: http://sandbox.onlinephpfunctions.com/code/8d38b83063d8480eb2443b1dfb0a4a83512b546b

Relevant documentation: https://www.php.net/manual/en/control-structures.break.php

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement