1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
 
//=============================================================
//=== Class: Settings Bitwise
//=== Dependancies: none 
//=== Author: VBAssassin ( www.coderprofile.com/coder/VBAssassin ) 
//=== Version: 1.00.00
//=== PHP version: 5.X
//=== License: GNU
//=== Copyright: 2009, Scott Thompson, All Rights Reserved
//=== Description: Specify an array of settings, and depending on the combination of settings will return a unique integer. This integer can then be used to workout the settings that were selected to produce that integer.
//=== Use Cases: A settings page for a users profile, or anywhere else where basic settings can be used such as "select the mailing lists you are interested in"...
//=============================================================
class settings_bitwise {
       
       //=============================================================
       //=== store the lookup table that will be bitwised against
       //=============================================================
       private $lookup_table = array();
       
       //=============================================================
       //=== set the lookup table
       //=============================================================
       public function set_lookup_table(array $table) {
              $this->lookup_table = $table;
       }
       
       //=============================================================
       //=== returns all possible numbers that could include the spefic setting
       //=============================================================
       public function get_all_combinations_of($specific_setting_value) {
              
              //contains a list of all possible integers that will contain those settings
              $possible_values = array();
              
              //maximum possible value
              $max_value = 0;
              foreach ($this->lookup_table as $number) {
                     $max_value += $number;
              }
              
              //scan through every number and see which contain those specific bits
              for ($counter = $specific_setting_value; $counter <= $max_value; $counter++) {
                     //when does this number contain these settings?
                     if ($specific_setting_value == ($specific_setting_value & $counter)) {
                            $possible_values[] = $counter;
                     }
              }
              
              //send back an array of the possible values
              return $possible_values;
              
       }
       //=============================================================
       //=== get the settings that are enabled/disabled by comparing the settings table with the integer $setting_value
       //=============================================================
       public function get_settings($setting_value) {
              
              //get a copy of the settings lookup table
              $applied_settings = $this->lookup_table;
              
              //loop through the lookup table to see which settings are active
              foreach ($this->lookup_table as $current_setting=>$value) {
                     
                     //check if the setting is enabled or not
                     if ($setting_value & $value) {
                            $applied_settings[$current_setting] = true;
                            } else {
                            $applied_settings[$current_setting] = false;
                     }
                     
              }
              
              //return the new settings table, bit this time with true/false for settings that were enabled/disabled
              return $applied_settings;
              
       }
       
}
 
//=============================================================
//=== Examples of how to use the class
//=============================================================
 
//lets create an instance of the bitwise settings
$settings = new settings_bitwise();
 
//now we have 4 settings, each setting must stay in the same order, and each setting is given a value that is in par with the binary system
$settings->set_lookup_table(
       array(
              'show_name' => 1,              //0001 = 1
              'show_age' => 2,              //0010 = 2
              'show_gender' => 4,              //0100 = 4
              'show_partner' => 8              //1000 = 8
              )
       );
 
//now the user has selected everything, so add up the numbers: 1 + 2 + 4 + 8 = 15. Store 15 in the database, since thats what the users settings are... and is used below to regenerate the same settings.
print_r($settings->get_settings(15));
/* PRINTS
Array
(
    [show_name] => true
    [show_age] => true
    [show_gender] => true
    [show_partner] => true
)
*/
 
//now the user has selected everything, so add up the numbers: 2 + 4 = 6. Store 6 in the database, since thats what the users settings are... and is used below to regenerate the same settings.
print_r($settings->get_settings(6));
/* PRINTS
Array
(
    [show_name] => false
    [show_age] => true
    [show_gender] => true
    [show_partner] => false
)
*/
 
//now the user has selected everything, so add up the numbers: 2 + 8 = 10. Store 0 in the database, since thats what the users settings are... and is used below to regenerate the same settings.
print_r($settings->get_settings(10));
/* PRINTS
Array
(
    [show_name] => false
    [show_age] => true
    [show_gender] => false
    [show_partner] => true
)
*/
 
//Now lets say we wanted to get every user out of a database that has a specific group of settings turned to on... then you use this to get every number that has those settings on :-) very usefull for a database search. The xample below returns every number that has the setting that corresponds to 4 in it.
 
print_r($settings->get_all_combinations_of(4));
/* PRINTS
Array
(
    [0] => 4
    [1] => 5
    [2] => 6
    [3] => 7
    [4] => 12
    [5] => 13
    [6] => 14
    [7] => 15
)
*/
?>