I have a multidimensional array and I have echoed the keys and values in a foreach loop. I would like to have an update box next to the 'id' (key) to update the id value and also next to the 'size' (key) to update the size value
Problem:: Bellow is my code, the input boxes echo out the right values for each key, but when I hit the update button, it doenst update...
The update doesnt have to be in a foreach loop, btw. I just thought it will be easier
Thanks in advance for all the help
CODE
<?php
session_start();
$array=array(
'Homer' => Array
(
'id' => 111,
'size' => 54
),
'Marge' => Array
(
'id' => 222,
'size' => 12
),
'Bart' => Array
(
'id' => 333,
'size' => 3
)
);
// update if submit
if (isset($_POST["submit"]))
{
// i tired a number of things here but it was all errors
}
echo "<form method='post' action=''>";
// put the array in a session variable
if(!isset($_SESSION['simpsons']))
$_SESSION['simpsons']=$array;
// getting each array in a foreach loop
foreach( $_SESSION['simpsons'] as $character => $info) {
echo $character.': id is '.$info['id'].', size is '.$info['size'];
//add and update input box for each ' id ' and ' size '
?>
<!-- input for id -->
<input name="<?php $character ?>" value="<?php echo $info['id'] ?>">
<!-- input for size-->
<input name="<?php $character ?>" value="<?php echo $info['size'] ?>">
<?php
echo"<br/>";
}
?>
<!-- submit button for the form -->
<input class="inputbox" type="submit" value="Update value of key" name="submit"/>
</form>
echo
in your inputname
attribute. and need to setname
attribute as array. So you input tag look like<input name="<?php echo $character ?>[]" value="<?php echo $info['id'] ?>">
.[]
brackets