Reputation: 31
I have searched for a few weeks now and am not able to find a solution to my problem. I've tried to understand what is written in the PHP Manual, but the examples don't match my issue. I've come close, specifically on this site, including a question similar to this one, but for a table display, but the questions and examples are enough different than my problem that I am not able to interpret those answers and modify them to my code. Or they are so advanced as to be over my head.
I need help, please...
My problem is trying to create a set of dynamic text boxes in a form, the number of sets based on user input. I then want to be able to keep those values entered on the form if there is a POST error. I can do this with all other fields on the form, but not the multidimensional array.
The set of text boxes are:
gFirstName, gLastName, gEmail
If a user enters a value of "3", then I need 3 rows of text boxes for gFirstName, gLastName, gEmail to display on the form.
If a user enters a value of "5", then I need 5 rows of text boxes for gFirstName, gLastName,gEmail to display on the form.
I can do a very simple echo display of the values, which work, but when I try and reproduce those values in the textbox form, I display the associative key or the numeric array value, or even worse, the word "array".
In addition, I try to check if the array is set or empty, so as not to get the dreaded offset or undefined error, yet I still get one of those, depending on how I modify the statement.
sigh....
Here is the multidimensional array display that does work:
$guestData = array
(
"gFirstName" => array("cindy","cheryl","john"),
"gLastName" => array("johnson","smith","jones"),
"gEmail" => array("[email protected]","[email protected]","[email protected]"),
);
foreach($guestData as $x => $gkey)
foreach($gkey as $key => $x_value)
{
if ($x_value<> ' ')
{
echo "Guest Data values are: x gkey is: " . $x . " and key is: " . $key . " and x_value is: ". $x_value ." <br> ";
}
next($guestData);
}
Here is the form code that I am trying to get to work:
if(is_array($guestData) and isset($guestData)) {
$keys = array_keys($guestData);
$count = count($guestData[$keys[0]]);
for($i=0;$i<$count;$i++){
$gnum = $key + 1;
echo "<label class=\"inq5a\">Guest $gnum First Name: <span class=\"error\">*</span> </label> ";
echo "<input class=\"textreg6\" type=\"text\" name=\'gFirstName\' value={$guestData['gFirstName'][$i]}</input>" ;
echo "<label class=\"inq5b\">Last Name : <span class=\"error\">*</span> </label>";
echo "<input class=\"textreg6c\" type=\"text\" name=\'gLastName\' value={$guestData['gLastName'][$i]} </input>" ;
echo "<label class=\"inq5b\">Email : <span class=\"error\">*</span> </label> ";
echo "<input class=\"textreg6c\" type=\"text\" name=\'gEmail\' value={$guestData['gEmail'][$i]}</input>" ;
}
next($guestData);
}
The result of the simple display is:
Simple Display: x gkey is: gFirstName and key is: 0 and x_value is: cindy
Simple Display: x gkey is: gFirstName and key is: 1 and x_value is: cheryl
Simple Display: x gkey is: gFirstName and key is: 2 and x_value is: john
Simple Display: x gkey is: gLastName and key is: 0 and x_value is: johnson
Simple Display: x gkey is: gLastName and key is: 1 and x_value is: smith
Simple Display: x gkey is: gLastName and key is: 2 and x_value is: jones
Simple Display: x gkey is: gEmail and key is: 0 and x_value is: [email protected]
Simple Display: x gkey is: gEmail and key is: 1 and x_value is: [email protected]
Simple Display: x gkey is: gEmail and key is: 2 and x_value is: [email protected]
The result of the form value only gives me the last value (key 2) and is shown below:
Guest 3 First Name: * john Last Name : * john Email : * john
Guest 3 First Name: * jones Last Name : * jones Email : * jones
Guest 3 First Name: * [email protected] Last Name : * [email protected] Email : [email protected]
It seems to be taking the text value from the Input statement at the beginning of the line.
So to summarize the problem:
I am not able to correctly populate the text form with an existing array value. The array is only displaying the values from the final "row" of the array group, not all the groups as it should.
Thank you for whatever help you can give me.
Upvotes: 2
Views: 3141
Reputation: 4005
When you output the form you do not have the curly brackets after the foreach loop. So php executes only the first statement in foreach loop. And all the other statements are executed only once when the foreach loop has already finished. That is why the variables all have the last value.
Upvotes: 0
Reputation: 3814
<?php
$guestData = array(
"gFirstName" => array("cindy", "cheryl","john"),
"gLastName" => array("johnson","smith", "jones"),
"gEmail" => array("[email protected]","[email protected]","[email protected]"),
);
if(is_array($guestData) and isset($guestData)) {
$keys = array_keys($guestData);
$count = count($guestData[$keys[0]]);
for($i=0;$i<$count;$i++){
$gnum = $i + 1;
echo "<label class=\"inq5a\">Guest $gnum First Name: <span class=\"error\">*</span> </label> ";
echo "<input class=\"textreg6\" type=\"text\" name=\'gFirstName\' value={$guestData['gFirstName'][$i]} </input>" ;
echo "<label class=\"inq5b\">Last Name : <span class=\"error\">*</span> </label>";
echo "<input class=\"textreg6c\" type=\"text\" name=\'gLastName\' value={$guestData['gLastName'][$i]} </input>" ;
echo "<label class=\"inq5b\">Email : <span class=\"error\">*</span> </label> ";
echo "<input class=\"textreg6c\" type=\"text\" name=\'gEmail\' value={$guestData['gEmail'][$i]} </input>" ;
echo "<BR><BR>";
}
}
This will give you the output you're expecting.
You should think about coming up with a better solution to your $guestData
array, or possibly transitioning to using a database or even a flat file format
Also notice how I'm setting the value for the inputs: {$guestData['gFirstName'][$i]}
Upvotes: 1