Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts

    Print info from .csv file.

    For a currency converter.

    Yahoo has a .csv file, 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?

    <div> - putting your mind in a box since 1997

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Could try
    PHP Code:
    // fopen etc here
    $splitted explode(",",$file);
    echo 
    $splitted[0] * $number .' ';
    echo 
    round($splitted[1],2); 

  • #3
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    LOL!

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

    Can ya help a brotha out?
    <div> - putting your mind in a box since 1997

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Ohh ok sorry lol Assumed you knew.

    Give me a few mins

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    <?php
    $filelocation 
    "quotes.csv";
    $number 20;
    $handle fopen($filelocation,"r");
    $splitted fgetcsv($handlefilesize($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

  • #6
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    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 Code:
    <?php
    $filelocation 
    "http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv";
    $number 20;
    $handle fopen($filelocation,"r");
    $splitted fgetcsv($handlefilesize($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
    <div> - putting your mind in a box since 1997

  • #7
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Ahh ok, sorry didn't realise you was getting it straight from the site, thought you was downloading it then wanting it read.

    PHP Code:
    <?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($handle2000",");
    echo 
    'Price: '.round($splitted[1]*$number,2).' ';
    echo 
    'Date: '.$splitted[2];
    ?>

  • #8
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    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 Code:
    <?php
    $filelocation 
    "http://download.finance.yahoo.com/d/quotes.csv?s=USDMXN=X&f=sl1d1t1ba&e=.csv";
    $handle fopen($filelocation,"r");
    $splitted fgetcsv($handle2000",");
    ?>


    <?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>'?>
    Last edited by ArcticFox; 03-22-2007 at 02:47 PM.
    <div> - putting your mind in a box since 1997

  • #9
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm trying something similar. Doesn't work. /quote.php?symbol=msft

    Help. Thx


    PHP Code:
    <?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>";

    ?>
    Last edited by sagrada; 04-08-2007 at 11:30 AM.

  • #10
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •