Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-18-2005, 01:31 AM   PM User | #1
doni
Regular Coder

 
Join Date: Oct 2002
Posts: 172
Thanks: 1
Thanked 0 Times in 0 Posts
doni is an unknown quantity at this point
"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
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.

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
If the items 3 and 5 are selected and all others are NOT, then the return value is 20.

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
Many programming languages include a function to decide which options are selected.

Last edited by doni; 01-18-2005 at 01:35 AM..
doni is offline   Reply With Quote
Old 01-18-2005, 08:29 AM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
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
raf is offline   Reply With Quote
Old 01-19-2005, 05:51 AM   PM User | #3
doni
Regular Coder

 
Join Date: Oct 2002
Posts: 172
Thanks: 1
Thanked 0 Times in 0 Posts
doni is an unknown quantity at this point
Quote:
Originally Posted by raf
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.

Quote:
Originally Posted by raf
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.
doni is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:51 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.