我有一个显示股票价格的表格,但我想在页面上自动更新股票价格,而无需用户刷新页面。我试过这个
$(document).ready(function(){
var updater = setTimeout(function(){
$('.table #price').load('index.php', 'update=true');
},6);
});
这似乎可行,但它将整个表格放在单元格中。我只想刷新价格单元格。任何帮助将不胜感激,因为我是 JavaScript 新手。我也在这里发布其他文件。
这是portfolio.php
<table class="table table-hover center-table table-bordered">
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>Total</th>
</tr>
<?php foreach ($shares as $row): ?>
<tr>
<td><?= $row["symbol"]?></td>
<td><?= $row["name"]?></td>
<td><?= $row["shares"]?></td>
<td id="price">$<?= number_format($row["price"],2)?></td>
<td>$<?= number_format($row["total"],2)?></td>
</tr>
<? endforeach ?>
<tr>
<td>CASH</td>
<td></td>
<td></td>
<td></td>
<td>$<?= number_format($cash[0]["cash"], 2)?></td>
</tr>
</table>
<script type="text/javascript" src="js/update.js" ></script>
这是 index.php
<?php
// configuration
require("../includes/config.php");
//query user's portfolio
$rows = query("SELECT * FROM shares WHERE id = ?", $_SESSION["id"]);
$cash = query("SELECT cash FROM users WHERE id = ?", $_SESSION["id"]);
//create array to store the shares
$shares = [];
//for each of the user info
foreach($rows as $row){
//lookup stock info
$stock = lookup($row["symbol"]);
if($stock !== false){
$shares[] = [
"name" => $stock["name"],
"price" => $stock["price"],
"shares" => $row["shares"],
"symbol" => $row["symbol"],
"total" => $row["shares"]*$stock["price"]
];
}
}
// render portfolio
render("portfolio.php", ["shares" => $shares,"cash" => $cash, "title" => "Portfolio"] );
?>