-5

I managed to save the checked boxes using php (implode) and Mysql in a table. The values are divided with comma. You can see something similar here: PHP Multiple Checkbox Array. So, I have a table like this:

Id                Values
1                  1,3

It was the best solution at that time. Now I want to let user to change his options. I need to show him back the checkbox group, with boxes 1 and 3 checked. Thank you!

Community
  • 1
  • 1
Mugur Ungureanu
  • 161
  • 1
  • 11
  • 1
    Please don't say Hurrah for learning something bad :) You shouldn't have that data in one row separated with a comma if `Now I want to let user to change his options` – Hanky Panky Nov 12 '14 at 07:17
  • Thank you for your comment. I will know better next time. In that moment I wasn't interested in let user to change his options and there are not only 3 but 78. At that moment it was a good option. – Mugur Ungureanu Nov 12 '14 at 07:20

1 Answers1

1
$checkbox_array = explode(',', $row['Values']);
for ($i = 0; $i < $max_checkboxes; $i++) {
    $checked = in_array($i, $checkbox_array) ? 'checked' : '';
    echo "<input type='checkbox' value='$i' $checked> $i <br>";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you. Of course! implode - explode. I can work my page from here. More useful your answer than saying that I am stupid! :) – Mugur Ungureanu Nov 12 '14 at 07:26
  • Nope nobody said you are stupid. The method you used for storing multiple values was not in accordance with common practices let alone good code standards and you were simply advised of that. You're free to decide whether to take or ignore that piece of advice but saying bad code does not imply you are stupid. – Hanky Panky Nov 12 '14 at 08:39