Bug #16654 Updated: A special question related to variable variables
| From: | sander@php.net | Date: | Wed, 17 Apr 2002 10:16:24 +0000 |
| Subject: | Bug #16654 Updated: A special question related to variable variables | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-5182@lists.php.net to get a copy of this message | ||
ID: 16654
Updated by: sander@php.net
Reported By: liquid@corroded.de
-Status: Open
+Status: Bogus
Bug Type: Variables related
Operating System: Windows 2k
PHP Version: 4.1.2
New Comment:
The bug system is not the appropriate forum for asking support
questions. For a list of a range of more appropriate places to ask
for help using PHP, please visit http://www.php.net/support.php
Previous Comments:
------------------------------------------------------------------------
[2002-04-17 04:47:48] liquid@corroded.de
You will find the question on the bottom of this message!
The situation:
Let's say that I have 2 or more forms and want to store the posted
values in a php-include-file. This might look like this:
1. Display the 1st form. The user enters some values and submits the
form.
2. The script now stores the values in a file which can later be
included by PHP.
3. Display the 2nd form with 2 submit buttons (back and next). The user
enters values again, then presses the back button.
4. 1st we include the file we have stored in "2", then display the
form, filled out with the values from the included file.
Ok, now the problem in detail:
The used forms may vary, each time the program executed, so the
algorythm for storing the values needs to be kind of flexible. That's
the count of values to store is diffenrent for each of the forms.
To make it easier, I use similar names for the variables names (var_1,
var_2 ... var_n) in all of the forms.
Some code for further explanation:
(I'm using old style parameters to make it simpler here.)
<pre>
// File form1.inc.php:
<?php
include("functions.inc.php");
storeValues($prevpage);
include("values1.inc.php");
?>
<form action="index.php" method="post">
<input type="text" name="prevpage" value="1" />
value1: <input type="text" name="var_1" value="<?php echo $var_1;
?>" size="30" maxlength="30" /><br />
value2: <input type="text" name="var_2" value="<?php echo $var_2;
?>" size="30" maxlength="30" /><br />
...
<input type="submit" value="Next">
</form>
// end File
// File form2.inc.php:
<?php
include("functions.inc.php");
storeValues($prevpage);
include("values2.inc.php");
?>
<form action="index.php" method="post">
<input type="text" name="prevpage" value="2" />
value1: <input type="text" name="var_1" value="<?php echo $var_1;
?>" size="30" maxlength="30" /><br />
value2: <input type="text" name="var_2" value="<?php echo $var_2;
?>" size="30" maxlength="30" /><br />
...
<input type="submit" value="Back" name="back">
<input type="submit" value="Next" name="next">
</form>
// end File
// File functions.inc.php:
<?php
function storeValues($page)
{
$fp = "values$page.inc.php";
if(file_exists($fp)){
$fp_new = "values$page.inc.new";
if($fh = fopen($fp, "r")){
if($fh_new = fopen($fp_new, "w")){
while(!feof($fh)){
$ln = fgets($fh, 1024);
// HERE COMES THE TRICK!!
if(ereg("var_([0-9]{1,2})", $ln, $var)){
$vvar = "var_{$var[1]}"; // variable variable
global $$vvar; // globalize $var_x from previously
submitted form
$tmp = "$";
$tmp .= "$vvar = ${$vvar};\n";
}
fputs($fh_new, $ln);
}
fclose($fh_new);
}
fclose($fh);
}
if(file_exists($fp_new)){
unlink($fp);
rename($fp_new, $fp);
}
}
}
?>
// end File
// File values1.inc.php or values2.inc.php ...
<?php
$val_1 = "xyz";
$val_2 = "zyx";
...
?>
// end File
// File index.php
<?php
// find the button used to submit
if(isset($next)) // button was "next"
$page = $prevpage + 1;
else if(isset($back)) // button was "back"
$page = $prevpage - 1;
else
$page = 1;
include("form$page.inc.php");
?>
// end File
</pre>
I hope you find the source code useful, because its not the original
code I'm working on. Well it's quite similar.
Summary:
I have a defined variable and set a variable variable to the same name
as the defined one, then I'm globalizing it.
The question i want to ask, is about the function "storeValues". Please
have a look at it and tell me if the "TRICK" I'm using is ok with PHP?
I want to make sure that it will still work with future versions of
PHP.
It looks a bit weird to me... But ok, it's working well with PHP 4.0.6
I did not find any useful documentation about it, so I needed to ask
that here.
c ya
Liquid
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=16654&edit=1