Checkbox loaded from the bank

Asked

Viewed 31 times

-2

I’m having 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 checkbox selected and saves with # separating the numbers. Ex: 0#1#2#3#4#5#6#. I want to return this value of the bank and tell that that chekbox that has some of these values is marked.

1 answer

3


try something like that:

//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>';
}

Stayed like this

inserir a descrição da imagem aqui

  • Excellent, saved my life. Thank you!

Browser other questions tagged

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