I’m trying to fill a list with the output from a mysql query. I can deliver 1 item but not more.
PHP code: ('fetchBooks.php')
$con = mysqli_connect(HOST,USERNAME,PASSWORD,DB);
$brand = $_GET['brand'];
$res = mysqli_query($con, "CALL GetBooks('$brand')");
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('brand'=>$row[0]));
}
echo json_encode(array('result'=>$result));
Javascript code:
$.getJSON(
'fetchBooks.php',
'brand='+ brand,
function(result){
const olEle = document.createElement("ol");
olEle.setAttribute("id", "tomes");
document.body.appendChild(olEle);
$.each(result.result, function(){
const liEle = document.createElement("li");
const aEle = document.createElement('a');
aEle.innerText = this['brand'];
liEle.append(aEle);
olEle.append(liEle);
});
}
);
All this works OK but if I try:
array_push($result, array('brand'=>$row[0]), $row[1]); or
array_push($result, array('brand'=>$row[0]), 'brand'=> $row[1]);
I can’t ever seem to get more than one item in the output. I’ve kicked a few other ideas around but I need to have control over each of the elements of the output so I can position them separately on the webpage eg Title, Author, Description etc I'm aware that the '$.each' may only apply to the whole output of each record not 'each' element of the record. Any ideas or help welcome!