1

I have these checkboxes on my website and user choices the appropriate one for himself. What is the best way to store it in the database? For example, user selects "Bluetooth", "Camera", "Sunroof" and "Taxi". And I insert their value as an array to the database like "2 3 7 15". How can I later echo them and make them to shows on the page like "Bluetooth Camera Sunroof Taxi"? Or do you have any other ideas? Thanks in advance!

P.s: As my website will be multilanguage, for this reason, I cannot store just string values.

enter image description here

5
  • Add all these options to a database table called "options", where each option is a record with an option ID. Use a pivot table to add records for users who have specific options, where each pivot table record represents that the user has selected an option. Commented Dec 13, 2017 at 8:09
  • who says that if you store them as string in the database you will break the multi-language ?? you can store it as string language_key and use this key in the language array $lang['language_key'] = 'The actual message to be shown'; ! Commented Dec 13, 2017 at 8:13
  • Too many checkboxes. I recommend using multiselect dropdown instead Commented Dec 13, 2017 at 8:17
  • @Maraboc it means if I write value="<?php echo lang("language_key"); ?>" it will insert the word itself,not the code, yes? Commented Dec 13, 2017 at 8:18
  • Yes, but before that you have the load the language you want use ! Commented Dec 13, 2017 at 8:41

1 Answer 1

2

At the time of store you can make a string of id's by using implode like,

$checkboxes = $this->input->post('check[]');
$check_array = implode(',',$checkboxes);

it makes a string of id's for you like 1,2,3 and so on... and store it in one column.

then at the time of retrieval you can explode that and make an array

explode(',',$check_array);

for display you can match the values with this array and if match found put it selected. I hope it helps. thank you.

Sign up to request clarification or add additional context in comments.

3 Comments

it means for every checkbox I should write an if statement, yes?
nop i think you can make a foreach for this array explode(',',$check_array); and check with every value and if match found make it selected.
<?php foreach(explode(',',$check_array) as $selected){ ?> <input type="checkbox" name="bluetooth" value="1" <?php echo ($selected == '1' ? 'checked' : null); ?>>Bluetooth <input type="checkbox" name="radio" value="2" <?php echo ($selected == '2' ? 'checked' : null); ?>>Radio <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.