Welcome Guest, Not a member yet? Register   Sign In
Save specific fields set in database

Hi! Lets say I have a table with data, for example

Table - Users


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?


(This post was last modified: 03-23-2016, 12:29 PM by Wouter60.)

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 />
<?php echo  form_checkbox('userfields[]','cb_last_name');?> Last name<br />
(Use the form helper to use these functions to build fields)

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[]
$saved_options serialize($checked_options);  //converts the array into a specially formatted string 

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
$checked_options unserialize($saved_options);  //convert the string back to an array
<?php echo  form_checkbox('userfields[]','cb_first_name',in_array('cb_first_name',$checked_options));?> First name<br />
<?php echo  form_checkbox('userfields[]','cb_last_name',in_array('cb_last_name',$checked_options));?> Last name<br />

The third parameter of the form_checkbox function makes the checkbox checked (true) or unchecked (false).

(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:


Then another table that linked the user_id to the profile_fields (only if set of course) and a column for public or not


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,


Many thanks guys for your answers!It was very helpful.

Have a nice day!

Theme © iAndrew 2016 - Forum software by © MyBB