I am trying to convert an array from PHP to Javascript. My table has a lot of columns. The first 2 are id (INT) and name (VARCHAR).
This is my PHP code:
$sql = 'select * from foodlist';
$query1 = mysqli_query($conn, $sql);
$javascriptarray = mysqli_fetch_array($query1);
This is my javascript code:
var foodArray = <?php echo json_encode($javascriptarray) ?>;
First I tried this:
jQuery(this).val(foodArray.id);
It outputs the id of the first row. How do I produce the id of the second row? Normally I would do something like this, but this produces nothing.
jQuery(this).val(foodArray[0]);
Thank you in advance for the help and tips.
foodArray.id[1]
if you're only looking for the id on the second row. Otherwise, if you want the id from all rows, you would do that with a for loop.