[英]How to auto load page/div using jquery ajax to the data within foreach loop?
直到現在,我仍將這些代碼停留在這里,我知道Ajax的代碼。這是錯誤的。
<script>
$(document).ready(function() {
$.ajaxSetup({ cache: false });
setInterval(function() {
$('#divToRefresh').load('userCount.php', function(data) {
$.each($(data), function(key, value) {
alert(value);
});
});
}, 3000);
});
</script>
這里的userCount.php
$get_temp = "SELECT * FROM temperature_setting WHERE device_key = 'YMR200'";
$result = $conn->query($get_temp);
while($row = $result->fetch_assoc()) {
$temp_id[] = $row['temp_id'];
}
foreach ($temp_id as $id_value) {
$get_current_value = "SELECT $id_value FROM data_current WHERE device_key = 'YMR200'";
$result_value = $conn->query($get_current_value);
$row_value = $result_value->fetch_assoc();
$value[] = $row_value[$id_value];
}
print_r($value);
有15個值,因此每個值都應轉到每個值
foreach ($temp_id as $id_value) { ?>
<p id="divToRefresh"></p>
<br>
<?php } ?>
根據目前的userCount輸出是
Array ( [0] => 65 [1] => -8 [2] => -5 [3] => -3 [4] => -5 [5] => 25 [6] => -5 [7] => -5 [8] => -5 [9] => -5 [10] => -5 [11] => -5 [12] => -5 [13] => 25 [14] => -5 )
我想要的是將這些價值放在每個
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
<p id="divToRefresh"></p>
<br>
也許這就是你想要的
$(document).ready(function() { setInterval(function() { $.ajax({ type: 'get', url: 'userCount.php', cache: false }).done(function(data) { $.each($(data), function(key, value) { $('#itemsTableBody').append($('<tr>').append($('<td>').text(value))); }); }); }, 3000); });
<html> <body> <table> <thead> <tr> <td>Item</td> </tr> </thead> <tbody id="itemsTableBody"></tbody> <tfoot> <tr> <td>Item</td> </tr> </tfoot> </table> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.