Jump to content

Outputting array


dreampho

Recommended Posts

Hi. I am trying to grab the live exchange rates from Yahoo.

 

I am able to get the first rate (GBP-USD) but cant output the second (GBP-EUR).

 

When I echo out out $results[1]; it says 'Message: Undefined offset: 1'.

 

Can anyone show me what I am doing wrong here?

 

<?php

$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1&s=GBPUSD=X+GBPEUR=X';
$handle = fopen($url, 'r');

if ($handle) {
$result = fgetcsv($handle);
fclose($handle);
}

echo $result[0];
echo $result[1];

?>

Link to comment
https://forums.phpfreaks.com/topic/273666-outputting-array/
Share on other sites

The data is being returned with each requested item as a separate line, and each call to fgetcsv only reads one line -

 

<?php

$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1&s=GBPUSD=X+GBPEUR=X';
$handle = fopen($url, 'r');

if ($handle) {
$usd = fgetcsv($handle);
$eur = fgetcsv($handle);
fclose($handle);
}

echo $usd[0];
echo $eur[0];

?>

Link to comment
https://forums.phpfreaks.com/topic/273666-outputting-array/#findComment-1408363
Share on other sites

Thank you, I see what the issue was now!

 

Would it be best to just use this method to do a math function with the rate, or would it be better to store the rates in a db, then on each load, check the rate, if the rate hasnt changed use the rate saved in the db, else overwrite with the new rate... but I am wondering, if each load it has be checked anyone, surely storing in a db would be worse?

Link to comment
https://forums.phpfreaks.com/topic/273666-outputting-array/#findComment-1408378
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.