我使用jquery从PHP文件中获取一些数据。我获取数据但它只输出获取表中的第一行。 这是我的PHP代码
$data = array();
while ($rowtwo = $queryresult->fetch_assoc()){
$data['id'] = json_encode($rowtwo['id']);
$data['Name'] = json_encode($rowtwo['Name']);
$data['Surname'] = json_encode($rowtwo['Surname']);
$data['Time'] = json_encode($rowtwo['Time']);
$data['Date'] = json_encode($rowtwo['Date']);
}
echo json_encode($data);
那就是我的jQuery代码
$(document).ready(function(){
setInterval(function(){
$.getJSON("http://www.blablabla.com/phpfile.php", function (data){
$("#Id").html( data.id );
$("#Name").html( data.Name);
$("#Surname").html( data.Surname);
$("#Time").html( data.Time );
$("#Date").html( data.Date );
});
},1000);
});
结果只有一行,但在我的表中我有5行。
答案 0 :(得分:0)
问题是您要替换$data
上的每个值。您必须推送$data
数组
$data = array();
while ($rowtwo = $queryresult->fetch_assoc()){
$temp = array();
$temp['id'] = $rowtwo['id'];
$temp['Name'] = $rowtwo['Name'];
$temp['Surname'] = $rowtwo['Surname'];
$temp['Time'] = $rowtwo['Time'];
$temp['Date'] = $rowtwo['Date'];
$data[] = $temp;
}
echo json_encode($data);
在您的jQuery
代码上。你必须循环。
<table>
<tbody id="mydangertwo">
</tbody>
</table>
$(document).ready(function(){
setInterval(function(){
$.getJSON("http://www.blablabla.com/phpfile.php", function (data){
var htmlText = "";
for ( var key in data ) {
//Access each entry using the variables below
//data[ key ].id
//data[ key ].Name
//data[ key ].Surname
//data[ key ].Time
//data[ key ].Date
htmlText += "<tr class='danger'>";
htmlText += "<td>" + data[ key ].id + "</td>";
htmlText += "<td>" + data[ key ].Name + "</td>";
htmlText += "<td>" + data[ key ].Surname + "</td>";
htmlText += "<td>" + data[ key ].Time + "</td>";
htmlText += "<td>" + data[ key ].Date + "</td>";
htmlText += "</tr> ";
}
//Append to mydangertwo
$( "#mydangertwo" ).append( htmlText );
});
},1000);
});
答案 1 :(得分:0)