【发布时间】:2014-09-14 21:21:24
【问题描述】:
我是 php 和 ajax 的新手,我一直在尝试创建一个实时仪表板,它可以在不刷新页面的情况下从 mysql 数据库进行更新。我已经能够在下面的代码中创建它,但是我正在努力调整它,以便它可以与多个 sql 查询和不同的 Div 一起使用。
目前我的代码运行 sql 查询并输出到 OrderSubmitted Div ID。我想向 div Id Orders Fulfilled 输出另一个 sql 查询,我该怎么做呢?
HTML
<div class="row">
<!-- Left Box -->
<div class="col-lg-6 col-sm-6" style="padding-right:15px; padding-left:0px">
<div class="well" style="height: 300px;">
<h2 style="text-align:center; font-size:8em;">
<div id="SubmittedOrders"></div>
</h2>
<p style="text-align:center; font-size:1.5em;">Outstanding Orders</p>
</div>
</div>
<!-- Right Box -->
<div class="col-lg-6 col-sm-6" style="padding-left:15px; padding-right:0px">
<div class="well" style="height: 300px;">
<h2 style="text-align:center; font-size:8em;">
<div id ="OrderFulfilled"></div>
</h2>
<p style="text-align:center; font-size:1.5em;">Orders fulfiled today</p>
</div>
</div>
</div>
AJAX 脚本
function getOrderStatus() {
?>
<script>
// IE5/6 support
function ajax_loop() {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// Create function for handling AJAX response
// Set dashboard div's contents to the response for Submitted Orders
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("SubmittedOrders").innerHTML = xmlhttp.responseText;
setTimeout(ajax_loop, 1000);
}
}
// Send AJAX request
xmlhttp.open("POST","getOrderStatus.php");
xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
xmlhttp.send("action=update");
}
ajax_loop();
</script>
<?php
}
getOrderStatus Php 文件
<?php
// Query users table
$sql = "SELECT COUNT(STATUS) from CE_TRANSACTIONS where STATUS = 0;";
// Execute query
$result = mysqli_query( $link, $sql );
// Loop through rows and draw table
while ( $row = mysqli_fetch_assoc($result) ) {
foreach ( $row as $field ) {
echo "$field";
}
}
【问题讨论】:
标签: php mysql ajax live dashboard