2014-01-29 20 views
1

我正在使用ajax和php獲取雅虎財經的實時值,即ftse。 我想以數組格式存儲所有值,並且想比較最近2次最近更新的值。如何將更新後的股票數據存儲在動態數組中

下面是我的javascript

<script> 
function loadXMLDoc() 
{ 
    var xmlhttp; 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
{ 
document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
} 
} 
xmlhttp.open("GET","ftse.php",true); 
xmlhttp.send(); 
} 
setInterval(loadXMLDoc,1000); 
</script> 

以下是我PHP代碼

<div id="myDiv"> 


<?php 
// Setup Variables 
$stockList = "^ftse"; 
$f = "l1"; 
$host = "http://finance.yahoo.com/d/quotes.csv"; 
$requestUrl = $host."?s=".$stockList."&f=".$f."&e=.csv"; 

// Pull data (download CSV as file) 
$filesize=2000; 
$handle = fopen($requestUrl, "r"); 
$raw = fread($handle, $filesize); 
fclose($handle); 

// Split results, trim way the extra line break at the end 
$quotes = explode("\n\n",trim($raw)); 

foreach($quotes as $quoteraw) { 
$quoteraw = str_replace(", I", " I", $quoteraw); 
$quote = explode(",", $quoteraw); 

// output the first element of the array, the Company Name 
} 


echo "<br><center><font size='30' color='green'>".$raw."<br>"; 


?> 

</div> 

以下是我ftse.php代碼。

<?php 
// Setup Variables 
$stockList = "^ftse"; 
$f = "l1"; 
$host = "http://finance.yahoo.com/d/quotes.csv"; 
$requestUrl = $host."?s=".$stockList."&f=".$f."&e=.csv"; 

// Pull data (download CSV as file) 
$filesize=2000; 
$handle = fopen($requestUrl, "r"); 
$raw = fread($handle, $filesize); 
fclose($handle); 

// Split results, trim way the extra line break at the end 
$quotes = explode("\n\n",trim($raw)); 

foreach($quotes as $quoteraw) { 
$quoteraw = str_replace(", I", " I", $quoteraw); 
$quote = explode(",", $quoteraw); 

// output the first element of the array, the Company Name 
} 

echo "<br><center><font size='30' color='green'>".$raw."<br>"; 

?>

問題是,如果該值越小則應該在紅色被印刷,並且如果大於在綠色。

回答

0

元素上沒有屬性color。你想設置一個樣式設置顏色爲綠色,所以你可以這樣寫:

echo "<br><center><font size='30' style='color=green'>".$raw."<br>"; 

理想情況下,你不會這麼做,因爲它只是污染了DOM。與類註釋:

echo "<br><center><font size='30' class='up'>".$raw."<br>"; 
echo "<br><center><font size='30' class='down'>".$raw."<br>"; 

然後在你的CSS文件,你只需要:

.up { 
    color: green 
} 

.down { 
    color: red 
} 

當你在這,也動了字體大小和其他風格的裝飾到CSS。

我希望這會有所幫助。

+0

感謝您的回覆。你有什麼建議是正確的,但我們不需要打印綠色和紅色的結果。我們必須根據優先值從中打印一個值,即如果值是低的,那麼紅色否則是綠色的,我們必須將這個值與以前的值進行比較。請你指導我如何比較這些價值。首先感謝 –

+0

,你想比較哪兩個變量?這只是在回聲之前放置一個if的問題,如果是第一個示例行,或者第二個回聲行如果是down,那麼它會在哪裏回顯。 – bitoiu

+0

我想比較單個變量的兩個值。這裏的ftse值保持不變,新的值是每次存儲的值。我想比較這些值,即新值與過去最近的值,並根據該值我想用綠色或紅色打印。 –