I have a simple array that I am trying to handle. It consists of two parts. A group of vid_ids and a single element called page. I was initially using a for loop to go through just the vid_id array but now since I added the page element I am not quite sure how to handle it. Anyone have any ideas
PHP
$array=array();
$sql = 'SELECT * from video LIMIT 0,9';
$stmt = $conn->prepare($sql);
$result=$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$array[]=htmlspecialchars( $row['vid_id'], ENT_NOQUOTES, 'UTF-8' );
}
$data['vid_array']=$array;
$data['page']=1;
$out = json_encode($data);
print $out;
Javascript
$.getJSON("load_live.php?t=" + (new Date()), function(json) {
for(vid_array.i=0; i < json.length; vid_array.i++) {
$('#element').append('<div id="'+json.vid_array[i]+'"></div>');
}
$('#element').append(json.page);
});
Json Output
{“vid_array”:[“3si7bxwk8ftpnvsw1gcf6giyxxch46sn”,”jch0dfepnu9hn0uw3m283g429mcj63q6″,”arh3xeklxjlztffxtybnucfnzosetgr8″,”6duy7xxj274w2cmhauth1tj933ck5i6z”,”9xx6pefa52s38ez77s5nbhz6jywbohc6″,”4eslxxl4etj5smhhr3wymc6pr8kys10c”,”4db1hp47be4auel633okbg6l884u0ef5″,”incwb9juxug8h986r9xt738ru4n0camd”,”vmsoqx03vppflh646b3au6jrlzt6mkej”],”page”:1}
Looks like your JavaScript for-loop is a little off. Try this
Also, as mentioned in my comment, you don’t need to use
htmlspecialchars()there. Simply assign thevid_idvalue to the array