Home > OS >  Problem trying to display array content in a table?
Problem trying to display array content in a table?

Time:03-19

Hello I have the following problem

This is how my site looks right now;

Site

I want to display the content in a table from an array located in a JSON file. I wanted to do this in an foreach loop and it is working just fine. Until I add a new one with array_push Because then I get all kind of errors

It will add a new row and place for items but they wont be filled because of these errors;

Errors

I know for sure that the content is getting added to the json file with my array_push with the exact same keys, yet they dont recognise it.

I found out myself that when I add something to the array it will add an "0", "1", "2", etc. in front of it. When I delete the number myself it will work in the table but it will automatically keep adding this to the array;

"0":{"Aquaman":{"Naam":"Aquaman","Power1":"Water","Power2":"Vissen","Power3":"Zwemmen","Power4":"Onderwater ademen"}}

How can I keep it from doing this? Or what other fixes are there for this problem?

PHP/HTML;

<?php
                                             
// ========== Read an array from a file ===================

// Open the file in 'read' modus
$file = fopen('myfile.json','r');                           

// Read the JSON array from the file
$aJSONArray = file_get_contents('myfile.json');  
           
// Convert to JSON array back to a PHP array
$arrDCSuperheroes = json_decode($aJSONArray,TRUE);

 // Close the file again            
fclose($file);                                              

// ========== Add a new value to the array ==============

if(!empty($_POST)) {
    $strSuperheldNaam = $_POST['strSuperheldNaam'];
    $arrDCSuperheroes2[$strSuperheldNaam] = array();
    $arrDCSuperheroes2[$strSuperheldNaam]['Naam'] = $_POST['strSuperheldNaam'];
    $arrDCSuperheroes2[$strSuperheldNaam]['Power1'] = $_POST['strPower1'];
    $arrDCSuperheroes2[$strSuperheldNaam]['Power2'] = $_POST['strPower2'];
    $arrDCSuperheroes2[$strSuperheldNaam]['Power3'] = $_POST['strPower3'];
    $arrDCSuperheroes2[$strSuperheldNaam]['Power4'] = $_POST['strPower4'];
    
    array_push($arrDCSuperheroes, $arrDCSuperheroes2);

    var_dump($arrDCSuperheroes);
}

// ========== Saving an array to a file =================

// Use JSON to encode the array into a storeable string
$aJSONArray = json_encode($arrDCSuperheroes);

// Open the file in 'write' modus
$file = fopen('myfile.json','w');    

// Save the content of the JSON array into the file
file_put_contents('myfile.json', $aJSONArray);              

// Close the file
fclose($file); 

echo("
    <html>

        <head>
            <title>Hello World!</title>
        </head>

        <body>

            <table border=1 width='90%'>
            <tr><th>Naam</th><th>Power 1</th><th>Power 2</th><th>Power 3</th><th>Power 4</th></tr>
        ");

                

                foreach($arrDCSuperheroes as $arrDCSuperheroesList) {
                    
                    echo("<tr><td>".$arrDCSuperheroesList['Naam']."</td><td>".$arrDCSuperheroesList['Power1']."</td><td>".$arrDCSuperheroesList['Power2']."</td><td>".$arrDCSuperheroesList['Power3']."</td><td>".$arrDCSuperheroesList['Power4']."</td></tr>");

                }   

     
            echo(" </table>

            <h3>Toevoegen aan array!</h3><hr>
                            
            <form method='post'>

                Naam: <input type='text' name='strSuperheldNaam'><br/>
                Power 1: <input type='text' name='strPower1'><br/>
                Power 2: <input type='text' name='strPower2'><br/>
                Power 3: <input type='text' name='strPower3'><br/>
                Power 4: <input type='text' name='strPower4'><br/><br/>
                                    
                <input type='submit' value='SUBMIT!'>

            </form>

        </body>

    </html>
");

?>

CodePudding user response:

You shouldnt use array_push on associative arrays. Assuming you want your json structure to be:

{
  "heroName": {
    "property": "value"
   },
   ...
}

You simply have to add the new hero by name with its properties like the following. Also you can move logic to save file inside the if statement to make sure the file is only written when updating it and not on every page load.


// ...

// Convert to JSON array back to a PHP array
$arrDCSuperheroes = json_decode($aJSONArray,TRUE);

if(!empty($_POST)) {
   $strSuperheldNaam = $_POST['strSuperheldNaam'];
   $arrDCSuperheroes[$strSuperheldNaam] = [
      'Naam' => $_POST['strSuperheldNaam'],
      'Power1' => $_POST['strPower1'],
      'Power2' => $_POST['strPower2'],
      'Power3' => $_POST['strPower3'],
      'Power4' => $_POST['strPower4'],
   ];


  // Use JSON to encode the array into a storeable string
  $aJSONArray = json_encode($arrDCSuperheroes);

  file_put_contents("myfile.json", $aJSONArray);
          
}

// ...

  • Related