Using multiple fopen or multiple fgetcsv

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mirefoot
Forum Newbie
Posts: 2
Joined: Wed Nov 04, 2009 5:32 am
Location: UK

Using multiple fopen or multiple fgetcsv

Post by mirefoot »

I am trying to read two csv files on the same page so that I can display the data from then on the page. But I only ever seem to see the data from the first file???

Code: Select all

<?php
 
$file_oil = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=CLZ09.NYM&f=sl1d1t1c1ohgv&e=.csv", "r");
 
while (!feof($file_oil) )
    {
    $oil_text = fgetcsv($file_oil, 1024);
    }
fclose($file_oil);
 
$file_gold = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=GCX09.CMX&f=sl1d1t1c1ohgv&e=.csv", "r");
while (!feof($file_gold) )
    {
    $gold_text = fgetcsv($file_gold, 1024);
    }
fclose($file_gold);
 
 
?>
And the full page

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
 
$file_oil = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=CLZ09.NYM&f=sl1d1t1c1ohgv&e=.csv", "r");
 
while (!feof($file_oil) )
    {
    $oil_text = fgetcsv($file_oil, 1024);
    }
fclose($file_oil);
 
$file_gold = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=GCX09.CMX&f=sl1d1t1c1ohgv&e=.csv", "r");
while (!feof($file_gold) )
    {
    $gold_text = fgetcsv($file_gold, 1024);
    }
fclose($file_gold);
 
 
?>
 
 
</head>
 
<body><table border="0"><tbody>
<tr style="text-align: center;">
<td>Oil Price <?php print $oil_text[1]; ?></td>
<td>Gold Price <?php print $gold_text[1]; ?></td>
</tr>
<tr>
<td><img src="http://www.oil-price.net/1q_small.gif" alt="Oil Prices for the Last Quarter" width="200" height="110" /></td>
<td><img src="http://www.gold-quote.net/1q_small.gif" alt="Gold Prices for the Last Quarter" width="200" height="110" /></td>
</tr>
</tbody></table>
</body>
</html>
mirefoot
Forum Newbie
Posts: 2
Joined: Wed Nov 04, 2009 5:32 am
Location: UK

Re: Using multiple fopen or multiple fgetcsv

Post by mirefoot »

Amazing I self fixed my problem. I don't know why this works but here is the code... If there is a better way of doing it please let me know.

Code: Select all

<?php
 
$file_oil = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=CLZ09.NYM&f=sl1d1t1c1ohgv&e=.csv", "r");
$file_gold = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=GCX09.CMX&f=sl1d1t1c1ohgv&e=.csv", "r");
while (!feof($file_oil) )
    {
    $oil_text = fgetcsv($file_oil, 1024);
    $gold_text = fgetcsv($file_gold, 1024);
    }
fclose($file_oil);
fclose($file_gold);
?>
Post Reply