Reputation: 545
I have a series of variables that need to take on one of two values based on a certain condition; in this case, a value pulled from a MySQL database. For simplicity, let's say the variables are $var1, $var2, $var3, etc. I have twenty variables set up like this, each attached to a form field. Right now, the logic for determining their values looks like this:
if($row['condition1'] == 0) //Value pulled from MySQL table {
$var1 = $_POST['var1']; //Value from a form field
} else {
$var1 = $_POST['var1hidden']; //Value from a hidden field
}
if($row['condition2'] == 0) //Value pulled from MySQL table {
$var2 = $_POST['var2']; //Value from a form field
} else {
$var2 = $_POST['var2hidden']; //Value from a hidden field
}
So this works, but I'd prefer not to have to copy it twenty times. Is there a way to loop through $var1-$var20, or am I stuck with this because each variable needs to be evaluated on its own?
Upvotes: 1
Views: 149
Reputation: 76636
The best solution would be to change your HTML form inputs into an array like structure using name[]
syntax. That way, you can get rid of the 20 different variables and manage user input efficiently.
However, you can dynamically define and compare variables using ${ }
syntax:
for ($i=1; $i <= 20; $i++) {
if($row['condition'.$i] == 0) {
${'var'.$i} = $_POST['var'.$i];
} else {
${'var'.$i} = $_POST['var'.$i.'hidden'];
}
}
Upvotes: 8
Reputation: 41
Chris i see you need this solution in order to fetch some POSTS vars. My advice is to make sure that on your HTML code you will rename all your input box from "var1", "var2", "var3" etc.. to "var[]"
HTML SIDE EXAMPLE
<input type="text" name="var[]" >
<input type="text" name="var[]" >
<input type="text" name="var[]" >
<input type="text" name="var[]" >
<input type="text" name="varhidden[]" >
<input type="text" name="varhidden[]" >
<input type="text" name="varhidden[]" >
<input type="text" name="varhidden[]" >
PHP SIDE EXAMPLE
$vars = $_POST[ "var" ];
$vars_hidden = $_POST[ "varhidden" ];
$vars_result = array();
if( !empty( $vars ) ){
for( $v = 0; $v < count( $vars ); $v++ ){
if( isset( $vars[ $v ] ) ){
$vars_result[ $v ] = $vars[ $v ];
} elseif( isset( $vars_hidden[ $v ] ) ){
$vars_result[ $v ] = $vars_hidden[ $v ];
} else {
$vars_result[ $v ] = NULL;
}
}
}
-->
?>
Upvotes: 0
Reputation: 26410
You don't need {
and }
:
if($row['condition1'] == 0) $var1 = $_POST['var1']; //Value from a form field
else $var1 = $_POST['var1hidden']; //Value from a hidden field
if($row['condition2'] == 0) $var2 = $_POST['var2']; //Value from a form field
else $var2 = $_POST['var2hidden']; //Value from a hidden field
You could do:
foreach ( $row as $key => $value ) {
if($value == 0) $$var.($key+1) = $_POST['var'.($key+1)]; //Value from a form field
else $$var.($key+1) = $_POST['var'.($key+1).'hidden']; //Value from a hidden field
}
Upvotes: 0
Reputation: 56
I think Amal is missing your $row var, and you can use a ternary operator for less code as follows:
for ($i=1; $i <= 20; $i++) {
$row['condition'.$i] == 0 ? ${'var'.$i} = $_POST['var'.$i] : ${'var'.$i} = $_POST['var' . $i . 'hidden'];
}
Upvotes: 0
Reputation: 1231
DO the simple okay:
$var=Array();
for($i=1;$i<=20;$i++){
if($row['condition'] == 0) //Value pulled from MySQL table {
$var[$i] = $_POST['var'.$i]; //Value from a form field
} else {
$var[$i] = $_POST['var'.$i.'hidden']; //Value from a hidden field
}
}
and access it by their index :
echo $var[1];
echo $var[2];
echo $var[3];
... // and etc
Upvotes: 1
Reputation: 1315
you can do this:
$i=1;
while ($i<=20){
if($row['condition[$i]']==0) {
$var[$i] = $_POST['var[$i]'];
} else {
$var[$i] = $_POST['var[$i]hidden'];
}
$i++;
} // END OF YOUR WHILE LOOP
Upvotes: 1
Reputation: 396
PHP supports "variable variables", using a double dollar sign syntax. For example:
$var1 = 'foo';
$name = 'var1';
echo $$name; // prints "foo"
http://php.net/manual/en/language.variables.variable.php
However, if you really have that many variables, it's probably better to store them in an array instead.
Upvotes: 1
Reputation: 562
i dont know if i understood your case at all but this would be my fast solution
$vars = array('var1' => 'condition1', 'var2' => 'condition2', 'var3' => 'condition3');
foreach ($vars as $var => $condition) {
if ($row[$condition] == 0) {
$$var = $_POST[$var];
} else {
$$var = $_POST[$var . 'hidden'];
}
}
save the vars and conditions in an array (the array structure can be different of course) and just loop through them
hope it helps
Upvotes: 1