Jump to content

form input to a multidimensional array


amelio

Recommended Posts

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]

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.