0

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>
5
  • You just forgot to write echo in your input name attribute. and need to set name attribute as array. So you input tag look like <input name="<?php echo $character ?>[]" value="<?php echo $info['id'] ?>">. Commented Nov 24, 2015 at 12:25
  • @hardiksolanki Even if he did he'd have two input elements with the same name which aren't a radio, so technically one is getting overwritten. He has to use [] brackets Commented Nov 24, 2015 at 12:28
  • @FlorianMüller check my updated answer. Commented Nov 24, 2015 at 12:29
  • @hardiksolanki Therefore I added the sentence about the brackets in the exact same moment ;D Commented Nov 24, 2015 at 12:29
  • 1
    @FlorianMüller: I have updated my answer before your comment :D Commented Nov 24, 2015 at 12:30

2 Answers 2

-1

You have two inputs which have the same name (twice <input name="Homer" ...>). I'd suggest a generic solution:

<input type="text" name="names[]" value="<?php echo $character;?>" />
<input type="text" name="ids[]" value="<?php echo $info['id'];?>" />
<input type="text" name="sizes[]" value="<?php echo $info['size'];?> />

Then you'll recieve in your PHP-Code the data via post (if you don't want to see the name as an input field, make it type="hidden").

<?php
$names = $_POST['names'];
$ids = $_POST['ids'];
$sizes = $_POST['sizes'];
foreach ($names as $key=>$value) {
    //name is $value
    //id is $ids[$key]
    //size is $sizes[$key]
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, It still doesnt allow me to update it. do I put $_SESSION['simpsons'][$character] = $ids; and $_SESSION['simpsons'][$character] = $size; in the foreach statement?
You are updating nothing in your question. If you want to update the session values, you may execute in my loop: $_SESSION['simpsons'][] = array('name'=$value,'id'=>$ids[$key],'size'=>$sizes[$key]);
-1

foreach should be

    foreach( $_SESSION['simpsons'] as $tt2) {
       print_r($tt2);
        foreach($tt2 as $character => $info){

       echo $character.': id is '.$info['id'].', size is '.$info['size'];
       //add and update input box for each ' id '      and    '  size '
      }
   }

4 Comments

Nothing prints using it that way :S
inside first foreach print_r($tt2);
Thanks :). but what about updating the size and id. i still can get it to work ;s
print_r($tt2) prints anything? what is prints?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.