Ladies and gents,
I found a very strange behavior which I cannot explain:
Assume that you have
multiple form elements on your page, maybe rendered by php
each form has one input field with an unique name
on the beginning of that page a session will be started
you store every posted input value in the $_SESSION variable
like this:
<?php
session_start();
$_SESSION["Test"] = "Hello";
foreach ($_POST as $name => $value) {
//echo "_POST: " . $name . ":" . $value . "<br>";
$_SESSION[$name] = $value;
//session_commit();
}
for ($i = 0; $i < 10; $i++) {
echo "<form action=\"multiform.php\" method=\"post\">Value for input $i: <input type=\"text\" name=\"input".$i."\"></form>\n";
}
print_r($_SESSION);
?>
If you use the above code, only the "Test" = "Hallo" will persist after the refresh of the page. Regardless which input value has been posted and stored into the session by the foreach, it will be gone after refresh.
Now the interesting part:
If you add a name to the form like this...
echo "<form name=\"form$i\" action=\"multiform.php\" method=\"post\">Value for input $i: <input type=\"text\" name=\"input".$i."\"></form>\n";
...the posted values will be stored then.
But why?
What has the form name to do with the persistence of the $_SESSION?
EDIT: If the input name only contains numbers, the problem seems to arraise:
<input type=\"text\" name=\"$i\">
Thanks for clarifyng this.
Jan
EDIT2:
If the accessor key for the $_SESSION array only contains numbers, php obviously does not persist the values, so something like this, won't be stored:
<?php
session_start();
for ($i = 1; $i < 10; $i++)
{
$_SESSION[$i] = "Hello $i";
}
?>
The confusing part is, if you do a
print_r($_SESSION)
just after the for loop, it will show 1-10 with Hello 1..10...
Though, after refresh it's gone...
The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore.
Found at http://php.net/manual/en/session.examples.basic.php
Could the problem be the integer as a form name as your edit comment suggests? If you serialize a form using PHP you end up with a variable which name is an integer and PHP's variable name cannot be a plain number. If your problem doesn't persist with a naming convention such as <input type=\"text\" name=\"sometext_$i\">, you should stop using plain numbers as a form element name.
It's also a good idea to give the form fields descriptive names. Form field named "1" or "2" doesn't really tell you anything about the containing value.
This is correct; you can not use a numeric-only key in $_SESSION. Trying to do so with error_reporting on highest level and display_errors set to true will yield a notice:
PHP Notice: Unknown: Skipping numeric key 0 in Unknown on line 0
It does store it in the $_SESSION array, but not actually in the session. Although strange behaviour, the notice is descriptive enough. The fix is easy, by the way, just create an array in $_SESSION['numbers'], for example.
<?php
session_start();
for ($i = 1; $i < 10; $i++) {
$_SESSION['numbers'][$i] = "Hello $i";
}
var_dump( $_SESSION['numbers'] );
Related
I need to use a variable as the key to access the $_GET data.
Is it possible?
This is my code:
if($_GET){
for($i=0;$i<9;$i++){
echo $_GET["value0"];
print_r(${'_GET["value'.$i.'"]'});
}
}
But it doesn't work.
I need to get $_GET['value0'], $_GET['value1'], and so on.
You should check the existence of each value with isset() before trying to access it. Also in php you don't need any complex string manipulations to put your variable in a string. You can just literally "put your variable in the string":
for($i = 0; $i < 9; $i++) {
if(isset($_GET["value$i"])) {
echo $_GET["value$i"];
}
}
I have 20 variables with name $encode1,$encode2....,$encode20.
Now, I want to print these variable in the for loop by combining $encode.$1 to achive variable $encode1.
Loop example:
for($i =1;$i<=20;$i++)
{
$echo = $encodedImage.$i; => What to do here?
}
How could I access the names by using iterator?
Plus, I don't want to create an array. I just want to access them directly dynamically.
I haven't found any answer on stackoverflow regarding this topic. If there is any, please share me the link. Thanks!
using variables variable to achieve such a approach
Sometimes it is convenient to be able to have variable variable names.
That is, a variable name which can be set and used dynamically. A
normal variable is set with a statement such as:
for($i = 1; $i <= 20; $i++) {
$myVariable = "encoded" . $i;
echo $$myVariable;
}
Use it this way.
Try this code snippet here
<?php
$encoded1=10;
$encoded2=20;
$encoded3=30;
for($i =1;$i<=3;$i++)
{
echo ${"encoded".$i};
}
I'm not even sure if what I am trying to do is possible, I have a simple php echo line as below..
<?php echo $T1R[0]['Site']; ?>
This works well but I want to make the "1" in the $T1R to be fluid, is it possible to do something like ..
<?php echo $T + '$row_ColNumC['ColNaumNo']' + R[0]['Site']; ?>
Where the 1 is replaced with the content of ColNaumNo i.e. the returned result might be..
<?php echo $T32R[0]['Site']; ?>
It is possible in PHP. The concept is called "variable variables".
The idea is simple: you generate the variable name you want to use and store it in another variable:
$name = 'T'.$row_ColNumC['ColNaumNo'].'R';
Pay attention to the string concatenation operator. PHP uses a dot (.) for this, not the plus sign (+).
If the value of $row_ColNumc['ColNaumNo'] is 32 then the value stored in variable $name is 'T32R';
You can then prepend the variable $name with an extra $ to use it as the name of another variable (indirection). The code echo($$name); prints the content of variable $T32R (if any).
If the variable $T32R stores an array then the syntax $$name[0] is ambiguous and the parser needs a hint to interpret it. It is well explained in the documentation page (of the variable variables):
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
You can do like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
$b = $$a;
echo "<pre>";
print_r($b[0]['Site']);
Or more simpler like this
$T1R[0]['Site'] = "test";
$c = 1;
$a = "T".$c."R";
echo "<pre>";
print_r(${$a}[0]['Site']);
If I have an array:
$resultArr = pg_fetch_array($result,NULL);
and at the top of my php code I declare:
$_SESSION['resultArr'] = $resultArr;
Why can't I access the array elements like so:
for($i = 0; $i < $NUM_COLUMNS; $i++){
// creation of the table and row are handled elsewhere.
// The table is also within a <form> if that matters
echo "<td>" .$_SESSION['resultArr'][$i]."</td>";
}
My table ends up having empty columns and I can't figure out why...
EDIT: I figured it out. I was declaring $_SESSION['resultArr'] = $resultArr; at the top of my code (right after session_start()) and it wasn't getting set. I moved it down to the point right after $resultArr = pg_fetch_array($result,NULL);
Is this how it's supposed to work or should it have worked fine at the top of the code?
Maybe you did and didn't mention, but you must call session_start() before any operation on $_SESSION
after your edit, yes this is how it is supposed to work, you first need to declare $resultArr, then put it's value in the session array
beacause in php you are no longer working with pointers,
$_SESSION['resultArr'] = $resultArr; mean "$_SESSION['resultArr'] takes all the values of $resultArr at that precise moment", but it does not mean "they are thesame, if one changes, then the other changes too" .
I am using PHP to create a form with an array of fields. Basically you can add an unlimited number of 'people' to the form and each person has a first name, last name, and phone number. The form requires that you add a phone number for the first person only. If you leave the phone number field blank on any others, the handler file is supposed to be programmed to use the phone number from the first person.
So, my fields are:
person[] - a hidden field with a value that is this person's primary key.
fname[] - an input field
lname[] - an input field
phone[] - an input field
My form handler looks like this:
$people = $_POST['person']
$counter = 0;
foreach($people as $person):
if($phone[$counter] == '') {
// use $phone[0]'s phone number
} else {
// use $phone[$counter] number
}
$counter = $counter + 1;
endforeach;
PHP doesn't like this though, it is throwing me an
Notice: Uninitialized string offset error.
I debugged it by running the is_array function on people, fname, lname, and phone and it returns true to being an array. I can also manually echo out $phone[2], etc. and get the correct value. I've also ran is_int on the $counter variable and it returned true, so I'm unsure why this isn't working as intended?
Any help would be great!
I am pretty sure phone[$counter] should be $phone[$counter]. Otherwise it will treat "phone" as a string.
var_dump your $_POST value and see what's going on. The array indicies probably aren't set to what you're expecting.