Page 1 of 1

The 'key - value' pair

Posted: Wed Sep 26, 2007 7:45 am
by Taras
I wonder if you can clarify something for me. In a PHP page collecting the data posted from a form I am using the following method:

Code: Select all

if (count($_POST)>0) {
		foreach($_POST as $key => $value)}
As far as I understand, the keys are turned into variables - am I correct here?

The following produces an output of the posted values:

Code: Select all

{
			echo $key, ": ", $value, "<br>";
						}
However, when I try to manipulate the variables I find that they have no values assigned to them.

Posted: Wed Sep 26, 2007 8:19 am
by urnetmate
Very True...the way u can read the values of Post variable is :

Code: Select all

$tmp_VariableName = $_POST['FormVariableName'];
All the best!

Re: The 'key - value' pair

Posted: Wed Sep 26, 2007 8:48 am
by volka
Taras wrote:However, when I try to manipulate the variables I find that they have no values assigned to them.
Meaning: you've showed us the the part of your script that is working but not the part that is not working. What are we supposed to say?

Posted: Wed Sep 26, 2007 9:05 am
by Taras

Code: Select all

<form method="POST" action = "test.php">
         
            <input type="text" size="10" name="year1">
            <input type="text" size="10" name="month1">
            <input type="text" size="10" name="day1">
            
<p><input type="submit" name="Sumbmit" value="Submit"></p>
So, in the following page I am expecting to see something from this code:

Code: Select all

echo $year1."-".$month."-".$day;
but I only get blanks.

If instead of using foreach I list all variables

Code: Select all

$year1 = $_POST["year1"]; 
$month1 = $_POST["month1"]; 
$day1 = $_POST["day1"];
then I can manipulate the variables.

My question is, if I use foreach to collect the data, will the elements appear as variables?

Posted: Wed Sep 26, 2007 9:15 am
by volka
Taras wrote:echo $year1."-".$month."-".$day;
either typo or missing 1 after $month and $day.


please try

Code: Select all

<html>
  <head><title>...</title></head>
  <body>
    <pre><?php print_r($_POST); ?></pre>
    <?php
    if ( isset($_POST["year1"], $_POST["month1"], $_POST["day1"]) ) {
      echo $_POST["year1"], '-', $_POST["month1"], '-', $_POST["day1"];
      
      $day1 = $_POST["day1"] + 5;
      echo '  ', $day1;
    }
    ?>
    <form method="POST" action="?">
      <div>
        <input type="text" size="10" name="year1">
        <input type="text" size="10" name="month1">
        <input type="text" size="10" name="day1">
        <br />
        <input type="submit" name="Sumbmit" value="Submit">
      </div>
    </form>
  </body>
</html>

Posted: Wed Sep 26, 2007 9:23 am
by Taras
sure, it should have been

Code: Select all

echo $year1."-".$month1."-".$day1;
As I said, I can get the variables if I address them one buy one, however, I need to add a number of fields to the form and this number is likely to increase so, instead of naming each single field, I wanted to process them all at once and turn them into variables. I assumed that the original method

Code: Select all

if (count($_POST)>0) { 
                foreach($_POST as $key => $value)}
would do that. My question was wether my assumption was incorrect and I need to do something else.

Posted: Wed Sep 26, 2007 9:37 am
by volka
I still do not understand what the problem is.

Maybe you want something like

Code: Select all

<html>
  <head><title>...</title></head>
  <body>
    <?php
    $t = '';
    $s = 0;
    foreach($_POST as $key=>$value) {
      $t .= $key;
      $s += $value;
    }
    echo $t, ' # ', $s, "<br />";
     ?>
    <form method="POST" action="?">
      <div>
        <input type="text" size="10" name="year1">
        <input type="text" size="10" name="month1">
        <input type="text" size="10" name="day1">
        <br />
        <input type="submit" name="Submit" value="Submit">
      </div>
    </form>
  </body>
</html>
may be you're interested in http://de2.php.net/extract <- I advise against it.
or maybe something completely different

Posted: Wed Sep 26, 2007 9:39 am
by someberry
Did you mean this, which is both not tested and not reccomended:

Code: Select all

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

Posted: Wed Sep 26, 2007 10:03 am
by Taras
someberry, yes, this is what I want to do. I am working on an in-house solution, it is quite safe to use this method. Basically, it grabs a bunch of dates and makes calculations based on those dates.

volka, would your method

Code: Select all

foreach($_POST as $key=>$value) { 
      $t .= $key; 
      $s += $value; 
    }
turn the keys into variables?

Posted: Wed Sep 26, 2007 10:10 am
by feyd
Take a look at the function volka linked to, however it is recommended to leave the data in the array.

Posted: Wed Sep 26, 2007 10:21 am
by volka
Taras wrote:volka, would your method

Code: Select all

foreach($_POST as $key=>$value) { 
      $t .= $key; 
      $s += $value; 
    }
turn the keys into variables?
No it uses the already existing variables/elements to do some..thing (useless, it's only an example).
There's really no need to pollute the variable tables with 1:1 copies. Trust feyd and me ;)