我有几个div标签,我想用几个不同的mysql查询进行更新。
下面我的代码只更新其中一个div,如何将其扩展为1-6,并为将来的更多div留出扩展空间?
我还需要能够使用特定表的特定查询单独更新每个div。
<html>
<div id="1"> </div>
<div id="2"> </div>
<div id="3"> </div>
<div id="4"> </div>
<div id="5"> </div>
<div id="6"> </div>
//...and so on
</html>
<script type="text/javascript">
$(document).ready(function () {
function display($id) {
$.ajax({
async: false,
type: "POST",
url: "apifile.php",
data: {id:$id},
dataType: "json",
success: function(msg) {
if(msg.success) {
$($id).next(".one").html(msg);
} else {
alert("error");
}
}
});
}
</script>
<?php
mysql_connect(host, user, pass);
mysql_select_db(name);
mysql_query("select round((count(*)*100)/(select count(*) from test),1) as percent from test group by field1 order by percent desc");
$reply['success'] = "Success";
if($return = display($id)) {
$reply['success'] = "Success";
} else {
$reply['error'] = "Error";
}
echo json_encode($reply);
?>
答案 0 :(得分:1)
您可以在div中添加一个类并在类
上运行ajax$( ".div" ).each(function() {
var div_id = $(this).attr('id');
display(div_id);
});
答案 1 :(得分:1)
您可以将每个查询中的data / messages / otherinfo存储在$ reply数组的不同部分
query1 goes here ...
$reply[1]['success'] = "Success";
if($return = display($id)) {
$reply[1]['success'] = "Success";
} else {
$reply[1]['error'] = "Error";
}
query2 goes here ...
$reply[2]['success'] = "Success";
$reply[2]['data'] = "3,4,5";
query3 goes here ...
$reply[3]['success'] = "Success";
$reply[3]['data'] = "beans,soup,guac";
echo json_encode($reply);