Question

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

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

?>

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

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top