I have a .php page that has variables, each associated with dozens of different search criteria. I thought it would be easiest to just jam all the variables into the database as one gigantic string.
Then I thought it would be even EASIER to jam the variables and their declarations with them as one giant string.
So what I mean is, instead of just this:
Stewie
101
Green
I would instead like to have this as one big blob in the database:
$user_name = 'Stewie';
$pants_size = '101';
$favorite_eggs = 'Green';
That way, I would be able to VERY easily capture and extract a large amount of user preference data without having to do much in the way of arrays. After all, I have to change the page often enough that altering arrays would be a headache.
If I could do this, I could simply and it would be able to simply redeclare all the variables all at once, and I wouldn't have to think about it any more.
The records don't need to be searchable, just extractable with the variable declaration (like $var='value'; with the quotemarks and all).
What I investigated so far is as follows:
Any help at all would be appreciated, even if it is to tell me, "This isn't possible. I recommend [XYZ]."
Thanks!
EDIT: dave e has a great solution below for retrieving such data, via eval. But I am still a bit stumped as to how to get that data into the database to begin with. Storing >100 variables and their declarations ($variable_name='value') with the same ease is a bit perplexing!
Make an aray of options like this:
$option_array['user_name'] = 'Stewie';
$option_array['pants_size'] = '101';
Use serialize to store the data
$stored_string = serialize($option_array);
when you get back data from db unserialize it $option_araay = unserialize($option_array);
!
Using a comment from http://www.php.net/manual/en/function.get-defined-vars.php#73608
Use this slightly adapted function
/**
* @desc works out the variables in the current scope(from where function was
* called). Returns a serialized version of an array with variable name
* as key and vaiable value as value
* @param $varList: variables returned by get_defined_vars() in desired scope.
* $excludeList: variables to be excluded from the list.
* @return string
*/
function get_refined_vars($varList, $excludeList) {
$temp1 = array_values(array_diff(array_keys($varList), $excludeList));
$temp2 = array();
while (list($key, $value) = each($temp1)) {
global $$value;
$temp2[$value] = $$value;
}
return serialize($temp2);
}
Then create the exclude list array
$excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'excludeList');
Define your variables
$user_name = 'Stewie';
$pants_size = '101';
$favorite_eggs = 'Green';
Now get all your define variables into a serialzied string
//get all variables defined in current scope
$varList = get_defined_vars();
// refine the list, serialized.
$storeThis = get_refined_vars($varList, $excludeList);
Store the $storeThis value in your mysql table
When you get the string out of mysql...
extract(unserialize($someStringFromDatabaseResult));
echo $user_name;
echo $pants_size;
echo $favorite_eggs'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With