I am trying to build a dropdown list from my sql database. Managed to get it work somewhat, but the list only shows the last item in the table, and i briefly get this printed on the page as the page reloads:
[
{"id":"1","zavod_ime":"PEIT"},
{"id":"2","zavod_ime":"PTJM"},
{"id":"3","zavod_ime":"PM"},
{"id":"4","zavod_ime":"PN"},
{"id":"5","zavod_ime":"BS"}
]
this is my separate php file which handles connecting and putting things in an array:
$sqltran = mysqli_query($con, "SELECT id, zavod_ime FROM zavod")or die(mysqli_error($con));
$arrVal = array();
while ($rowList = mysqli_fetch_array($sqltran)) {
$namez = array(
'id'=> $rowList['id'],
'zavod_ime'=> $rowList['zavod_ime']
);
array_push($arrVal, $namez);
}
echo json_encode($arrVal);
mysqli_close($con);
and the list part in the html:
<select>
<?php foreach($namez as $zavod): ?>
<option value="<?= $namez['id']; ?>"><?= $namez['zavod_ime']; ?></option>
<?php endforeach; ?>
</select>
ideas?
echo json_encode($arrVal, true);