douhan8009
2015-03-11 10:56
浏览 52
已采纳

使用jquery ajax检索并显示多个不同的mysql查询

I have several div tags that I would like to be updated with several different mysql queries.

My code below only updates one of these divs, how can I expand it to 1-6 and also leave room for expansion in the future for more divs?

I also need to be able to update each div individually with a specific query for a specific table.

<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);

?>

图片转代码服务由CSDN问答提供 功能建议

我有几个div标签,我想用几个不同的mysql查询进行更新。

下面我的代码只更新了其中一个div,如何将其扩展为1-6,并为将来的更多div留出扩展空间? \ n

我还需要能够使用特定表的特定查询单独更新每个div。

 &lt; html&gt; 
 
&lt; div id =“1”&gt;  &lt; / div&gt; 
&lt; div id =“2”&gt;  &lt; / div&gt; 
&lt; div id =“3”&gt;  &lt; / div&gt; 
&lt; div id =“4”&gt;  &lt; / div&gt; 
&lt; div id =“5”&gt;  &lt; / div&gt; 
&lt; div id =“6”&gt;  &lt; / div&gt; 
 // ...依此类推
 
&lt; / html&gt; 
   
 
 
   &lt; script type =“text / javascript”&gt; 
 
 $(document).ready(function(){
function display($ id){
 $ .ajax({
 async:false,
 键入:“POST”,
 url:“apifile.php”,
 data:{id:$ id},
 dataType:“json”,
 success:function(msg){
 if(msg。 成功){
 $($ id).next(“。one”)。html(msg); 
} else {
 alert(“error”); 
} 
} 
}); \  n} 
 
&lt; / script&gt; 
   
 
 
 &lt;?php 
 
mysql_connect(host,user,  pass); 
mysql_select_db(name); 
mysql_query(“select round((count(*)* 100)/(从test中选择count(*)),1)作为测试组的百分比by field1 order by percent desc”)  ; 
 
 $ reply ['success'] =“成功”; 
if($ return = display($ id)){
 $ reply ['success'] =“成功”; 
}其他{\  n $ reply ['error'] =“错误”; 
} 
echo json_encode($ reply); 
 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • douli2063 2015-03-11 11:15
    最佳回答

    you can store the data/messages/otherinfo from each query in different portions of your $reply array

    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);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题