1

我正在使用 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>";

?>

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

4

1 回答 1

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。

我希望这有帮助。

于 2014-01-29T12:31:41.697 回答