I have a php file, which I run through javascript. The php goes like this:
<?php
$con=mysqli_connect("localhost","root","****","*****");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM pictures");
foreach ($result as $row){
$return[]=array('number'=>$row['number'], 'path'=>$row['path']);
}
$dbh = null;
header('Content-type: application/json');
echo json_encode($return);
?>
When I run the php file in a browser directly, I get this response:
[{"number":"1","path":"blue.png"},{"number":"2","path":"red.png"},{"number":"3","path":"green.png"}...]
Then I have the javascript that goes like this:
var refreshImages = function() {
$.get( // JQuery function to execute an AJAX Get request
url: "get.php",
dataType: "json
success: function(data) {
data.forEach(function(src, i) {
var img_tag = $("#img_+i).find("img"); // Finds the correct img tag to update
if(img_tag.attr('src') != src) {
img_tag.attr('src', src);
}
}
}
}
window.setInterval(refreshImages, 1000); // Calls refreshImages every 1000ms
And with values I get from the php file I need to fill in this ul:
<ul>
<li id="img_1" class="image">first value should go here<span>10.000</span></li>
<li id="img_2" class="image">second value should go here<span>7.500</span></li>
<li id="img_3" class="image">third value should go here<span>5.000</span></li>
...
<li id="img_35" class="image">35th value should go here<span>5.000</span></li>
Can anyone show me where I'm wrong? I'm pretty sure it's the javascript but I have no idea...