gpt4 book ai didi

javascript - 如何在动态数组中存储更新的股票数据

转载 作者:行者123 更新时间:2023-11-28 08:34:20 26 4
gpt4 key购买 nike

我正在使用ajax和php从雅虎财经获取实时值(value),即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>";

?>

问题是,如果该值较小,则应以红色打印,如果较大,则应以绿色打印。

最佳答案

元素上没有属性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。

我希望这会有所帮助。

关于javascript - 如何在动态数组中存储更新的股票数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21430835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com