2013-03-27 79 views
-2

我想通過創建一個股票組合來學習php和javascript。我想要做的是自動更新表格的價格單元,而不使用JavaScript刷新頁面。我創建了一個新的php文件來獲取組合的價格字段,但我似乎無法使其工作。我在價格領域遇到很多錯誤。我已將所有文件放在這裏pastebin。任何幫助,將不勝感激。 enter image description here只傳遞一個值到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

我不再有任何錯誤,但現在我得到整個頁面在表格中的價格字段。

+2

請編輯的問題,並張貼一些代碼,你有任何錯誤消息。 – Jess 2013-03-27 15:56:13

+0

請勿使用jsFiddle或pastebin等外部服務託管您的代碼。代碼出現在問題中,然後你可以用一個工作示例來補充你的問題。 – meagar 2013-03-27 16:00:09

+0

對不起。用問題發佈代碼。 – tironci 2013-03-27 16:08:59

回答

1

你要傳遞的index.php中的參數,其中一個具有關鍵shares一個數組,然而在update.php你想只是傳回該數組,在update.php改變這種

render("portfolio.php", $share); 

render("portfolio.php", ['shares' => $share]); 
+0

當我這樣做時,我仍然收到錯誤消息。未定義的指數:第13行的portfolio.php中的符號未定義的指數:第14行的portfolio.php中的名稱我對股票,總數和現金都有同樣的看法。 – tironci 2013-03-27 16:27:53

+1

您將會爲update.php中的每個共享返回'$ row ['price']'。或者讓它匹配你在index.php中返回的數組,或者檢查這些值是否在嘗試在portfolio.php中使用之前設置。 – Crisp 2013-03-27 16:31:17

+0

我在update.php中匹配index.php中的數組,現在我沒有得到任何錯誤消息,但我得到整個頁面在表格中的價格單元格內。 – tironci 2013-03-27 16:46:20