What I'd like to do is have a function to get numerous variable names from an array and then pass them through a function which will define the variables. That is, I'd like to turn:
$lg_id = $_POST['lg_id'];
$lg_initials = $_POST['lg_initials'];
...
into a loop.
What I've got so far is:
// to get variable names which basically looks like:
// $sfn[0] = '';
// $sfn[1] = 'lg_id';
// $sfn[2] = 'lg_initials';
// ...
include("includes/arrays.inc");
// lose these as they're not in $_post
unset($sfn[0],$sfn[1],$sfn[30]);
// lose submit from $_POST
array_pop($_POST);
// get rid of http:// in URL
$_POST['site_address'] = ereg_replace("^http://", "", $_POST['site_address']);
// define multiple vars
// got the basis of this function from http://ca3.php.net/variables.external
function get_superglobal_vars_from_POST() {
$numargs = func_num_args();
$setargs = 0; // for counting set variables
for ($i=0; $i<$numargs; $i++) {
$varname=func_get_arg($i);
if (!isset($_POST[$varname])) {
$result='';
} else {
$result=$_POST[$varname];
$setargs++;
}
$GLOBALS[$varname]=$result;
}
//return $setargs; // who cares?
}
// put quotes around variable names
$headerswithquotes = "'" . implode ("','", $sfn) . "'";
// pass variable names as args to function
get_superglobal_vars_from_POST($headerswithquotes);
// check that they're defined and in correct order
echo '<pre>';
print_r($_POST);
echo '</pre>';
but that doesn't work.
What does seem to work is replacing:
get_superglobal_vars_from_POST($headerswithquotes);
with:
get_superglobal_vars_from_POST('lg_id','lg_initials'...);
but that defeats the purpose of getting the numerous var names from the array.
and when I:
print_r($headerswithquotes);
it spits out what I want in:
'lg_id','lg_initials'...
so why doesn't it work? I guess maybe b/c the function call doesn't like a var as args. But more importantly, how do I get what I want?
As an aside, I read about "return" in the php manual, but I still don't get why that person included it in the function. I commented it out because the author commented "who cares?", but could someone explain what it's supposed to do?
As always, thanks in advance!