Checkbox loaded from the bank

-2

I have a difficulty executing a logic, I have a table in the database that saves the values of an array, takes the values of one or more selected checkboxes and saves with # separating the numbers. Ex: 0 # 1 # 2 # 3 # 4 # 5 # 6 #. I want to return this value from the bank and tell that chekbox that has any of these values is marked.

    
asked by anonymous 30.08.2018 / 18:57

1 answer

3

try something like this:

//array o valor de todos inputs
$todos_itens = array(
'1' => 'Valor 1',
'2' => 'Valor 2',
'3' => 'Valor 3',
'4' => 'Valor 4',
'5' => 'Valor 5',
'6' => 'Valor 6',
'7' => 'Valor 7',
'8' => 'Valor 8',
'9' => 'Valor 9',
'10' => 'Valor 10'
);

//retorno do banco
$retorno_banco = '0#1#3#4#6';

//transforma o retorno em um array
$itens = explode('#',$retorno_banco);
foreach ($itens as $value) {
  $marcado[$value] = true;
}

//percorre todos os inputs verificando se existe o array $marcado
foreach ($todos_itens as $numero => $input) {
if($marcado[$numero]){
  echo '<input type="checkbox" checked name="input" value="'.$numero.'">'.$input;
}else{
  echo '<input type="checkbox" name="input" value="'.$numero.'">'.$input;
}
echo '<hr>';
}

It was like this

    
30.08.2018 / 19:18