Home > other >  How to use PHP to populate a <select> Dropdown with json data
How to use PHP to populate a <select> Dropdown with json data

Time:03-16

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": ""      
    }
  ]

CodePudding user response:

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>
  • Related