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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.