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

?>
  • 写回答

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀