...

View Full Version : Print info from .csv file.



ArcticFox
03-21-2007, 11:24 AM
For a currency converter.

Yahoo has a .csv file (http://download.finance.yahoo.com/d/quotes.csv?s=USDMXN=X&f=sl1d1t1ba&e=.csv), which at the moment looks like this:

"USDMXN=X",11.087,"3/21/2007","5:49am",11.037,11.137

I need to pull the information from the second and third places:
11.087 and 3/21/2007

What I'd like to do is be able to multiply the 11.087 by whatever number I choose and round it to the nearest cent.


Example:


Your Price: $29.95 (332.06 pesos)


29.95 x 11.087 = 332.05565


Is this something that I can do entirely with PHP or do I need JS? And how can this be done?

:)

Nightfire
03-21-2007, 11:40 AM
Could try


// fopen etc here
$splitted = explode(",",$file);
echo $splitted[0] * $number .' ';
echo round($splitted[1],2);

ArcticFox
03-21-2007, 11:52 AM
LOL!

That's not enough of a hint for me to work with this late in the morning.

Can ya help a brotha out? :D

Nightfire
03-21-2007, 11:55 AM
Ohh ok sorry lol Assumed you knew.

Give me a few mins

Nightfire
03-21-2007, 12:29 PM
<?php
$filelocation = "quotes.csv";
$number = 20;
$handle = fopen($filelocation,"r");
$splitted = fgetcsv($handle, filesize($filelocation), ",");
echo 'Price: '.round($splitted[1]*$number,2).' ';
echo 'Date: '.$splitted[2];
?>

That should work. $number is how much you want the amount multiplied by

ArcticFox
03-22-2007, 10:29 AM
That one is not finding information when I direct it to yahoo's page.


Warning: filesize() [function.filesize]: Stat failed for http ://quote.yahoo.com/d/quotes.csv?s=&f=sl1d1t1c1ohgv&e=.csv (errno=2 - No such file or directory) in curr2test.php on line 5

Line 5: $splitted = fgetcsv($handle, filesize($filelocation), ",");

Code as I have it:


<?php
$filelocation = "http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv";
$number = 20;
$handle = fopen($filelocation,"r");
$splitted = fgetcsv($handle, filesize($filelocation), ",");
echo 'Price: '.round($splitted[1]*$number,2).' ';
echo 'Date: '.$splitted[2];
?>




Interestingly enough, someone else is looking for the same thing:
http://www.codingforums.com/showthread.php?t=110372

Nightfire
03-22-2007, 12:38 PM
Ahh ok, sorry didn't realise you was getting it straight from the site, thought you was downloading it then wanting it read.



<?php
$filelocation = "http://download.finance.yahoo.com/d/quotes.csv?s=USDMXN=X&f=sl1d1t1ba&e=.csv";
$number = 20;
$handle = fopen($filelocation,"r");
$splitted = fgetcsv($handle, 2000, ",");
echo 'Price: '.round($splitted[1]*$number,2).' ';
echo 'Date: '.$splitted[2];
?>

ArcticFox
03-22-2007, 01:35 PM
That works! Thank you. :)

I noticed that this yahoo information is updated live, so I also included the time from their .csv file (I may change that later). Here's the result:


<?php
$filelocation = "http://download.finance.yahoo.com/d/quotes.csv?s=USDMXN=X&f=sl1d1t1ba&e=.csv";
$handle = fopen($filelocation,"r");
$splitted = fgetcsv($handle, 2000, ",");
?>


<?php
echo '<style>td {font-size:10px; text-align:center;} </style>';
echo '<table style="border:1px solid #000000; width:200px;"><tr><td colspan=2 style="border-bottom:1px solid #000000;">1 US Dollar = '.($splitted[1]*1).' Mexican Pesos</td></tr>';
echo '<tr><td>Date: '.$splitted[2]. '</td><td>';
echo 'Time: '.$splitted[3]. '</td></tr></table>';
?>


Price: $19.99 <?php echo '<span style="font-size:10px;">('.round($splitted[1]*19.99,2).' Pesos)</span>'; ?>

sagrada
04-08-2007, 10:23 AM
I'm trying something similar. Doesn't work. /quote.php?symbol=msft

Help. Thx



<?php

$symbol = str_replace(",", "+", $symbol);

$url = "http://download.finance.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv";
$fp = fopen($url , "r");
while (!feof($fp)) {
$data[] = fgetcsv($fp,1000,",");
}
fclose($fp);

array_splice ($data, -1);

$count = 1;

print "<div align=\"center\"><table border=\"0\" bgcolor=\"#4A8EB3\" cellspacing=\"0\" cellpadding=\"3\"><tr><td><table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n";

print "<tr bgcolor=\"#4A8EB3\"><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Symbol</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Price</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Date</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Time</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Change</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Open</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>High</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Low</b></font></td><td align=\"center\"><font face=\"Arial\" color=\"#ffffff\" size=\"2\"><b>Volume</b></font></td></tr>\n";

foreach ($data as $number => $data)
{

if ($count % 2) {


print "<tr bgcolor=\"#ffffff\">";
foreach ($data as $value)
{
print "<td align=\"center\"><font face=\"Arial\" size=\"2\">$value</font></td>";
}
print "</tr>";


} else {


print "<tr bgcolor=\"#EEEEEE\">";
foreach ($data as $value)
{
print "<td align=\"center\"><font face=\"Arial\" size=\"2\">$value</font></td>";
}
print "</tr>";

}

$count++;

}

print "</table></td></tr></table></div>";

?>

iLLin
04-08-2007, 02:45 PM
What does it do? You could use curl instead, I'm just not sure how fopen retrieves files, if use can use variables in the url or not?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum