我使用ajax和php从yahoo finance获取实时值,即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>";
&GT;
问题是如果值较小则应以红色打印,如果大于绿色则打印。
答案 0 :(得分: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。
我希望这会有所帮助。