dsg56465 2014-01-29 12:14
浏览 58
已采纳

如何在动态数组中存储更新的库存数据

I am fetching live value i.e. ftse from yahoo finance, using ajax and php. I want to store all value in an array format and want to compare last 2 recently updated values.

following is my 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>

following is my php code.

<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("

",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>

following is my ftse.php code.

<?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("

",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>";

?>

Problem is if the value is smaller then should be printed in red and if greater then in green color.

  • 写回答

1条回答 默认 最新

  • dongzangchui2072 2014-01-29 12:31
    关注

    There's no property color on the element. You want to set a style to set the color to green, so you would write:

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

    Ideally you wouldn't do this because it just pollutes the DOM. Annotate with a class:

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

    Then in your CSS file you just have:

    .up {
      color: green
    }
    
    .down {
      color: red
    }
    

    While you are at it, also move the font size and other style decorations to the CSS.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)