我想通過創建一個股票組合來學習php和javascript。我想要做的是自動更新表格的價格單元,而不使用JavaScript刷新頁面。我創建了一個新的php文件來獲取組合的價格字段,但我似乎無法使其工作。我在價格領域遇到很多錯誤。我已將所有文件放在這裏pastebin。任何幫助,將不勝感激。 只傳遞一個值到php文件
portfolio.php file
<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"]
];
//dump($shares);
}
}
// render portfolio
render("portfolio.php", ["shares" => $shares,"cash" => $cash, "title" => "Portfolio"]);
?>
update.php
<?php
require("../includes/functions.php");
require("../includes/config_update.php");
$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){
$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]);
?>
update.js
$(document).ready(function(){
var updater = setTimeout(function(){
$('#price').load('update.php', 'update=true');
},6);
});
當我運行此我首先得到整個標籤le在價格單元以及這些錯誤消息注意:未定義的變量:份額portfolio.php行10 提供的foreach()在portfolio.php第10行中的無效參數 未定義變量現金在portfolio.php在行31
我不再有任何錯誤,但現在我得到整個頁面在表格中的價格字段。
請編輯的問題,並張貼一些代碼,你有任何錯誤消息。 – Jess 2013-03-27 15:56:13
請勿使用jsFiddle或pastebin等外部服務託管您的代碼。代碼出現在問題中,然後你可以用一個工作示例來補充你的問題。 – meagar 2013-03-27 16:00:09
對不起。用問題發佈代碼。 – tironci 2013-03-27 16:08:59