amelio Posted January 12, 2010 Share Posted January 12, 2010 Hi, I am trying to bring form input into a multidimensional array. The way I have constructed the code seems to make sense to me as it would create an array for each table row. The result i get from submitting a few numbers is the attached image. I don't understand the result I am getting. If I insert a value into the into the first input of the table it should create a value in $input_array[1][1] which I then want to insert as the value in the same input field. Any help greatly appreciated. <?php if (isset ($_POST['input_array'])) { $input_array = $_POST['input_array']; }; ?> </head> <body> <?php $rows = 10; $cols = 5; echo "<table>"; for ($a=1;$a<=$rows;$a++){ echo "<tr>"; for ($b=1;$b<=$cols;$b++){ echo "<td> <form method=\"post\"><input type=\"text\" name=\"input_array[$a][$b]\" "; if (isset ($input_array[$a][$b])) { echo "value=\"$input_array[$a][$b]\" "; }; echo "/></td>"; }; echo "</tr>"; }; echo "</table>"; echo "<input type=\"submit\" value=\"submit\"/></form>"; ?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 13, 2010 Share Posted January 13, 2010 i had to run this script on my own webspace to work it out as your code looked fine to me. change: echo "value=\"$input_array[$a][$b]\" "; to: echo "value=\"".$input_array[$a][$b]."\" "; I don't know why, but I have always concatenated variables into string even if they are in double quotes and normally work fine. I think this is the reason I adopted the rule to always concatenate variables into strings like: $always = 'always'; print("Usually this will ".$always." work"); instead of: print("Usually this will $always work"); This is the section of the php manual to read for this topic: http://au.php.net/manual/en/language.types.string.php under section "Variable parsing" Quote Link to comment Share on other sites More sharing options...
amelio Posted January 13, 2010 Author Share Posted January 13, 2010 Thanks so much Catfish, that is it. I do normally concatanate variables but as I was echoing html I didn't think about it. I'm still trying to get my head round it though. It seems that when you just include the variabe in the string without concatenation it tells you what the variable is but when you take it out of the string it replaces the variable with the value. Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.