You can get, set, and delete individual variables using the methods described in Module variable get, set, delete. What do you do when you have a list of variables? Use the
_many version of each method.
For this example we have an important module named beer_fibre_content_calculator and in the module we create object $mv.
$mv = new module_variable('beer_fibre_content_calculator');
Set many
The following code sets variables namedale, lager, porter, stout to various values.
$mv->set_many(array('ale' => 'yes', 'lager' => 'no', 'porter' => 'no', 'stout' => 'no'));
Get many
The following code returns a list of variables and has default values for any missing variables.$list = $mv->get_many(array('ale' => '', 'porter' => 'maybe'));
print serialize($list);
Here is the result.
a:2:{s:3:"ale";s:3:"yes";s:6:"porter";s:2:"no";}
Get list
get_list is a simpler version of get_many with no default values. The following code returns the variables in the list.
print serialize($mv->get_list('porter', 'stout'));
Here is the result showing all the variables returned.
a:2:{s:6:"porter";s:2:"no";s:5:"stout";s:2:"no";}
get_list can accept arrays containing lists. The following code returns the variables in the lists.
print serialize($mv->get_list('porter', array('stout')));
Here is the result showing all the variables returned.
a:2:{s:6:"porter";s:2:"no";s:5:"stout";s:2:"no";}
Get all
The following code returns all the variables set for the module.print serialize($mv->get_all());Here is the result showing all the variables returned which, in this case, is all the variables set in an earlier paragraph.
a:4:{s:3:"ale";s:3:"yes";s:5:"lager";s:2:"no";s:6:"porter";s:2:"no";s:5:"stout";s:2:"no";}
Is
The following code returns true if a variable exists.print serialize($mv->is('stout'));
Here is the result.
b:1;





- Facebook Like
- Log in or register to post comments