From:             [EMAIL PROTECTED]
Operating system: Windows 2k
PHP version:      4.1.2
PHP Bug Type:     Variables related
Bug description:  A special question related to variable variables

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:
&lt;?php
include("functions.inc.php");
storeValues($prevpage);
include("values1.inc.php");
?>

&lt;form action="index.php" method="post">
&lt;input type="text" name="prevpage" value="1" />

value1: &lt;input type="text" name="var_1" value="<?php echo $var_1; ?>"
size="30" maxlength="30" /><br />

value2: &lt;input type="text" name="var_2" value="<?php echo $var_2; ?>"
size="30" maxlength="30" /><br />

...

&lt;input type="submit" value="Next">
&lt;/form>
// end File

// File form2.inc.php:
&lt;?php
include("functions.inc.php");
storeValues($prevpage);
include("values2.inc.php");
?>

&lt;form action="index.php" method="post">
&lt;input type="text" name="prevpage" value="2" />

value1: &lt;input type="text" name="var_1" value="<?php echo $var_1; ?>"
size="30" maxlength="30" /><br />

value2: &lt;input type="text" name="var_2" value="<?php echo $var_2; ?>"
size="30" maxlength="30" /><br />

...

&lt;input type="submit" value="Back" name="back">
&lt;input type="submit" value="Next" name="next">
&lt;/form>
// end File

// File functions.inc.php:
&lt;?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 ...
&lt;?php
$val_1 = "xyz";
$val_2 = "zyx";
...
?>
// end File

// File index.php
&lt;?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 bug report at http://bugs.php.net/?id=16654&edit=1
-- 
Fixed in CVS:        http://bugs.php.net/fix.php?id=16654&r=fixedcvs
Fixed in release:    http://bugs.php.net/fix.php?id=16654&r=alreadyfixed
Need backtrace:      http://bugs.php.net/fix.php?id=16654&r=needtrace
Try newer version:   http://bugs.php.net/fix.php?id=16654&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=16654&r=support
Expected behavior:   http://bugs.php.net/fix.php?id=16654&r=notwrong
Not enough info:     http://bugs.php.net/fix.php?id=16654&r=notenoughinfo
Submitted twice:     http://bugs.php.net/fix.php?id=16654&r=submittedtwice

Reply via email to