* You have a complex and evolving application.
* You have database entries that have an ever growing list of boolean attributes.
* The database is becomming too large and complex for so many true/false (boolean) field columns.
Example
* The company is in need of translators to handle the language translations that their product supports.
* You have a database that stores job applications.
* You need a way to store the selection of foreign languages the applicant is fluent in (i.e. as checkboxes).
* You need a way to easily change/edit the language selection for each applicant.
* You need a way to easily add more language options as the company's support for languages expands.
Solution 1 - Boolean Columns
Have a database column in the 'job_applicants' table with a Y/N or 1/0 enumerated type (ENUM) for every language the company supports.
The Problem With This Solution
* The database can quickly become cluttered with so many boolean columns.
* The database would require constant structure changes each time the company adds support for another language.
* The database could become large and inefficient.
Solution 2 - Bitmasks
The idea of Bitmasks may seem complex at first, but once you understand the logic, it's a breeze. Let's just cut straight to the point.
- Code: Select all
<?php
$languages = array(
'English' => 1,
'Spanish' => 2,
'German' => 4,
'Martian' => 8,
'Russian' => 16,
'Arabic' => 32,
'French' => 64,
'Chinese' => 128,
'Japanese' => 256,
);
$myLanguages = 85;
foreach ($languages as $language => $bitmask) {
if ($myLanguages & $bitmask) echo 'Yes - ' . $language . '<br />';
else echo 'No - ' . $language . '<br />';
}
?>
For those of you who are unable to actually test the code above, the code is simple enough to easily convert into your language of choice.
So what's going on? Well, if you actually run the code above, you'll get a result like this:
- Code: Select all
Yes - English
No - Spanish
Yes - German
No - Martian
Yes - Russian
No - Arabic
Yes - French
No - Chinese
No - Japanese
So how does it work? Well, rather than me explaining it, figure it out yourself.
1. Look at the yes/no result above.
2. Each language represents a number. Start your total at 0.
3. For each language that says 'No', do nothing.
4. For each language that says 'Yes', add the number representing that language to the total.
5. Here's what you'll end up with.
- Code: Select all
+ 1 English
+ 0 Spanish
+ 4 German
+ 0 Martian
+ 16 Russion
+ 0 Arabic
+ 64 French
+ 0 Chinese
+ 0 Japanese
= 85 TOTAL (the value of $myLanguages)
And there you have it. A single number stored in a database column can say a lot. Go ahead and drop those boolean columns, already.
Over and out.