How to use PHP to populate a Dropdown with json data

I am trying to populate a dropdown using php and a json file. It should get populated with all of the “Name” values. If I use:

$printers=file_get_contents ("config/printers.json");
$test= json_decode($printers,true);
foreach($test as $test) {
    echo $test['Name'];
}

It will display the correct values so I know the json is correct and using correct call to get the data.
(updated)If I use this as suggested I get an error “Fatal error, Uncaught TypeError: cannot access offset of type string on string in:” and points to the echo call.

<select id="printer" name="printer">
<option value="none">Select Printer</option>
<?php 
foreach($test as $t) {
   echo '<option value="'. $t['Name'].  '">'. $t['Name'].'</option>';      
  }
?>
</select>

But if I change it to (which is probably wrong):

<select id="printer" name="printer">
        <option value="none">Select Printer</option>
        <?php 
          foreach($test as $t) {
            echo '<option value="'. $test['Name'].  '">'. $test['Name'].'</option>';      
          }
        ?>
    </select>

The error goes away and the dropdown will get populated but with only 5 entries. (there are 20 elements in the array) and all 5 entries are the last item in the array.
I think I am missing something in escaping the quotes correctly? This part seems to be resolved?

The json file is:

[
    {
      "Name": "CR-10",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10 V2",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10 Mini",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10S",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10S4",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10S5",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""     
    },
    {
      "Name": "CR-10S Pro",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10S Pro V2",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-10 Max",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "CR-20",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 3",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 3X",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 3 V2",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 3 Pro",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 3 Pro/BLV Mod",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 5",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 5 Pro",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Ender 5 Plus",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Voxelab Aquila",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    },
    {
      "Name": "Anet ET4/ET5",
      "GantryStyle": 1,
      "comment": "",
      "image": "cr-10.jpg",
      "option1": ""      
    }
  ]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

you can use ' and " together:

  <select id="printer" name="printer">
        <option value="none">Select Printer</option>
        <?php 
          foreach($test as $t) {
            echo '<option value="'. $t['Name'].  '">'. $t['Name'].'</option>';      
          }
        ?>
    </select>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x