Save specific fields set in database |
Hi! Lets say I have a table with data, for example
Table - Users fields: 1)first_name 2)last_name 3)birthday 4)email 5)phone And lets say every user can set to show in his profile only 3 of that fields first user checked 1),3),4) second user checked 2),3),5) So what would be the best approach to save sets of fields for every user? Something like personal account setting to which fields to show on a site. How i see that, just save data like string with fields name which must be show for every user for example first_name|birthday|phone or email|phone ... and so on Any suggestions? Thanks!
If you have a form where the user can check the various options, then you only get the checked checkboxes in your $_POST array; unchecked checkboxed are not being posted.
I would suggest naming the checkboxes like this: PHP Code: <?php echo form_checkbox('userfields[]','cb_first_name');?> First name<br /> In your controller, proces the posted values like this: PHP Code: $checked_options = $this->input->post('userfields'); //creates an array from all fields named userfields[] Save the value $saved_options to your database in a text field. To display the checked options when you open the user settings form later on: PHP Code: $saved_options = $record->saved_options; //read the field from the text field in your table The third parameter of the form_checkbox function makes the checkbox checked (true) or unchecked (false).
03-23-2016, 06:04 PM
(This post was last modified: 03-23-2016, 06:10 PM by PaulD. Edit Reason: spelling mistake )
The following description might be a bit confusing to read, but I hope the jist of it gets across.
I would have a table with profile fields: profile_field_id profile_field_name Then another table that linked the user_id to the profile_fields (only if set of course) and a column for public or not user_profile_id user_profile_user_id user_profile_profile_id user_profile_public Then build your page option list from a query on the profile fields table, (left joined to user_profile_profile_id, where user_profile_user_id is your user) using a foreach, and then set value of each checkbox based on if the user_profile_public is set to 1 or not. 1. Now, in the future, you can add a profile setting, such as 'Favourite Song' just by adding an entry to the profile fields table. 2. You can delete one easily too by removing the entry in the profile fields table and anything in the user profile table by user_profile_profile_id 3. You can also check the settings individually easily, without decrypting a string, and pull out the settings with one quick query. 4. It is very easy, when displaying public info, to find out what info is to be displayed. 5. You do not have to update any views when adding new profile settings Whether that is the correct way, or a way that works for you, it is how I would approach it. Hope that suggestion helps in some way, Paul. |
Welcome Guest, Not a member yet? Register Sign In |