Reputation: 6673
I have a page with 24 different buttons.
When the user logs in I have to check if he has the right to click the button and activate that specific functionality. So what I thought is to store an array with 24 items made this way:
[0]=>'yes',
[1]=>'no
and so on for all the buttons.
When I retrieve this array from the db at login I store it in $_SESSION array and then for each button I can do
if($_SESSION['right'][0]=='yes'){
//show the button
}else{
//hide the button
}
The array will be stored in a table where there will be all the different user profiles (so each array will exist only once) and each user will have a join to the profile id.
What will be the best way to store the array in the db? Or I miss a better way to achieve my goal?
Upvotes: 0
Views: 62
Reputation: 4984
Take a look at bitmask approach
You can store up to 32 or 64 (on 64 bit systems) values in a single integer.
Each of your buttons would have a number 2^n where n is the button index. The resulting number is the sum of all active button numbers.
Upvotes: 1
Reputation: 11561
Don't store your data like that in a DB. That goes against the purpose of a DB.
Create a table for permissions with the user id as foreign key, and then add a INT(1) UNSIGNED
column for each permission. Zero means no, one means yes.
Upvotes: 0
Reputation: 51
I saw the same way in some CMS like Wordpress and OpenCart.
Array stored as serialized string. So, you can use serialize($array_value) to insert, and $array_value = unserialize($row['field1']) to select.
Upvotes: 0
Reputation: 100175
you can serialize() your array and insert it in a varchar field in mysql, and later when you need it, fetch it from db and unserialize() it. But much better way would be to make your tables relational.
Upvotes: 1