![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
Regular Coder ![]() Join Date: Oct 2002
Posts: 172
Thanks: 1
Thanked 0 Times in 0 Posts
![]() |
"Bitwise" Operator?
I've got a form that has about 10 "select fields" which all allow multiple selections. I need to store the information provided via the form in MySQL.
I'm thinking that the "multiple" options could be stored as a BIT value if there is some sort of BITWISE operator available for use when I go to do a query. Does anyone know if there is such an operator? Thanks! For those who don't know what I mean by a "Bit" value, I'll try and give a brief description. The first item in the list is 1, then the second item is 2. After that each item is DOUBLE the previous item. If an option is not selected, then the option's value is set to ZERO. All the values are added up. No matter what you do, there is only ONE combination of selected items that will add up to any given number. Code:
Option 1 = 1 Option 2 = 2 Option 3 = 4 Option 4 = 8 Option 5 = 16 Option 6 = 32 Option 7 = 64 Option 8 = 128 Total possible value = 255 If items one and two are selected and all other's are NOT selected, then the return value is 3. Code:
Option 1 = 1 Option 2 = 2 Option 3 = 0 Option 4 = 0 Option 5 = 0 Option 6 = 0 Option 7 = 0 Option 8 = 0 Returns 3 Code:
Option 1 = 0 Option 2 = 0 Option 3 = 4 Option 4 = 0 Option 5 = 16 Option 6 = 0 Option 7 = 0 Option 8 = 0 Returns 20 Last edited by doni; 01-18-2005 at 01:35 AM.. |
|
|
|
|
|
PM User | #2 |
|
Master Coder ![]() ![]() Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
i don't see what this has to do with MySQL... Turning the array with the selected values into one value needs to be done inside your applicationlayer (PHP?) and the result is then included in an insert or update query.
with PHP, you can do $bitvalue = array_sum($_POST['your_listbox']); assuming that the listbox is called 'your_listbox', and that the values for each option are 1,2,4,8 etc (--> you can assign the optionvalues automatically when you build the listbox with PHP) but it's not realy a practicle system. it's uch better to take a meaningful optionvalue (like a label or ID or so) and then store the selected options as a serialized array.
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html |
|
|
|
|
|
PM User | #3 | ||
|
Regular Coder ![]() Join Date: Oct 2002
Posts: 172
Thanks: 1
Thanked 0 Times in 0 Posts
![]() |
Quote:
Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|