• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Need help with setting variable variables in php

I do a lot of very large forms and one of the biggest pains is collecting all the form data and assigning them to variables.  I know I can just use the POST variables but sometimes it's just easier to manipulate $su rather to $_POST['su'].

So 2 questions:
1. How do I dynamically create a series of variables from an array of values (field names) and:
2. How to I access the created variables to check the data in the variable?

I've started a function but am stuck.  I'm not getting any errors but I don't know if it's just not doing anything or if it is working and I just need to figure out how to get to the data I'm putting in there.

In the code below, I'm just trying to create the variable and assign the fieldname I'm passing so I can check the value.  Ultimately, it will need to be something like:
${$arrFields[$i]} = $POST[$arrFields[$i];

-- so I can take the incoming values and assign them to variables.

Any help is appreciated - many thanks in advance.
function setVars ($fields){
	$arrFields = explode(",",$fields);
	for($i=0; $i< count($arrFields);$i++) {
		${$arrFields[$i]} = $arrFields[$i];
	}
}

Open in new window

0
saabStory
Asked:
saabStory
3 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
About the only way to get unknown variables out of an array is with a 'foreach' statement.  http://us3.php.net/manual/en/control-structures.foreach.php
0
 
Greg AlexanderLead DeveloperCommented:
Consider the attached:
<?
$_POST['test'] = 1;
$_POST['another'] = 2;
$_POST['last'] = 'last var field';

foreach($_POST as $key => $value){
	${$key} = $value;	
}

echo $test."<br>";
echo $another."<br>";
echo $last."<br>";
?>

Open in new window

0
 
Atique AnsariCommented:

<?php


$_POST['test'] = 'test value';
$_POST['another'] = 2;
$_POST['last'] = 'last var field';

// Converting an array to individual variables
extract($_POST);

echo $test.'<br><br>';
echo $another.'<br><br>';
echo $last.'<br><br>';

?>

Open in new window

0
 
saabStoryAuthor Commented:
Thanks for all the input - got me to do a lot of reading.  Couldn't get a darn thing to work and then I woke up this morning, sat down and just saw it clear as a bell.

I call the function and pass in the field names with:

setVars ('newGroup,goalsGroup,goalsText,disbandGroup,disbandText');

With the function below and it works just fine.  Saves me a boatload of time because I always keep a list of my field names as I build to plug into my SQL query to build the table.  All I need to do with this is add the commas and I can now build my variables quickly and output the form contents quickly using a similar function.

function setVars ($fields){
	$arrFields = explode(",",$fields);
	for ($i=0;$i<count($arrFields);$i++) {
		global ${$arrFields[$i]};
	${$arrFields[$i]} = $_POST[$arrFields[$i]];
	}
}

Open in new window

0
 
saabStoryAuthor Commented:
Thanks to all for getting me thinking and looking the right way.  This will be a huge help for my current and future projects.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now