PDA

View Full Version : "Bitwise" Operator?


doni
01-18-2005, 01:31 AM
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.


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


This is where the terms "8 Bit", "16 Bit", "32 Bit" & "64 Bit" come from. Data is stored pieces.

If items one and two are selected and all other's are NOT selected, then the return value is 3.


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


If the items 3 and 5 are selected and all others are NOT, then the return value is 20.


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


Many programming languages include a function to decide which options are selected.

raf
01-18-2005, 08:29 AM
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.

doni
01-19-2005, 05:51 AM
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.


I originally thought that it would be a MySQL issue because I was looking at eventually needing to do a query for records that meet the specified requirements. I realized this morning that I could figure out what the possible values would be at the PHP level and query for those values.


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.

The "true values" are rather long and I'd rather not have to worry about typing them in every time I need to use them. Representing "ALL options selected" would be easier as an integer value of 255 vs. a string of text that would be 200 characters long or an array of 10 strings each 10 characters long.