주가가 표시된 표가 있지만 사용자가 페이지를 새로 고침하지 않고 주가를 자동으로 업데이트하고 싶습니다. 나는 이것을 시도했다자바 스크립트로 표 셀을 새로 고치는 방법
$(document).ready(function(){
var updater = setTimeout(function(){
$('.table #price').load('index.php', 'update=true');
},6);
});
이것은 작동하는 것처럼 보일지라도 전체 테이블을 셀에 넣는다. 가격 세포를 새로 고치고 싶습니다. 필자는 JavaScript를 처음 접했을 때 어떤 도움을 주셨을 것입니다. 다른 파일도 여기 게시하고 있습니다.
이
<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>
portfolio.php 인이 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"]);
?>
나는 장님이되어야하지만 ID가 'price' 인 요소는 어디에 있습니까? – adeneo
a) id = price가 인 요소를 여러 개 가질 수는 없습니다. b) 단일 셀만 렌더링하는 별도의 PHP 페이지를 호출해야합니다. 동일한 index.php를 호출하면 모든 테이블이 셀 내에 렌더링됩니다. – Draykos
아, 이제 ID를 추가했습니다. 로드 함수가 서버에서 완전한 테이블을 리턴합니까? 아니면 여기서 어떤 문제점이 있습니까? – adeneo