1

I have a multidimensional array containing character names from the Simpsons (homver, Marge and bart) and I have echoed the keys and values in a foreach loop. I want to have two input boxes next to each character name, that updates the id and size of each specific name.

I have the code bellow. The values are showing inside the input box but they are not updating :(

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
)
);


////////////////////////////////////
if (isset($_POST["submit"])) {
 for ($i = 0; $i < count($_POST['names']); $i++) {
    $names = $_POST['names'][$i];
    $ids = $_POST['ids'][$i];
    $sizes = $_POST['sizes'][$i];
 }
}
////////////////////////////////////
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 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'];?>" />

<?php

echo"<br/>"; 
}

?>

<!-- submit button for the form -->
<input class="inputbox" type="submit" value="Update value of key"     name="submit"/>
</form>

1 Answer 1

1

The Issue

Let's take a look at what your code does when a user submits the form...

if (isset($_POST["submit"])) {
 for ($i = 0; $i < count($_POST['names']); $i++) {
    $names = $_POST['names'][$i];
    $ids = $_POST['ids'][$i];
    $sizes = $_POST['sizes'][$i];
 }
}

Great. You take the form data, and loop through each character in your simpsons array, and for each character set $names,$ids, and $sizes.

There are two issues that I see.

  1. The three variables you set that I named above, are not arrays, but hold single values for the specific character at that iteration in the loop.

  2. Nothing is done with the variables. The values are set for one character, and then overwritten for the next. Nothing is done with the data.

One Possible Solution

So let's do something with the data from the form. Here is my solution.

if (isset($_POST["submit"])) {
 $newArray = [];
 for ($i = 0; $i < count($_POST['names']); $i++) {
   $newArray[$_POST['names'][$i]] = [
     'id' => $_POST['ids'][$i],
     'size' => $_POST['sizes'][$i]
   ];
 }
 $_SESSION['simpsons'] = $newArray;
}

If the form is submitted, we make a new temporary array. We then add new sub arrays for each character with their set id and size. Then we put that array into the session.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow great explanation. Thank you so much! @Hurricane Development
May I get your email? I Would like to propose something

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.