Stock ticker help

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
novicephp
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2008 2:06 am

Stock ticker help

Post by novicephp »

Hello everyone i use the following script to pull data to display the stock on a website:

Code: Select all

<?php
error_reporting(0);
class getStock {
    function getQuote($stock) {
        $url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1c1p2&e=.csv", $stock);
        $fp  = @fopen($url, 'r');
        $data = @fgetcsv($fp, 1000, ', ');
        fclose($fp);
        $this->symbol = $data[0];
        $this->price = number_format($data[1],2);
        $this->change = number_format($data[2],2);
    $this->percent = number_format($data[3],1);
    }
}
 
$stocks = array('^DJI','^IXIC','^GSPC','^GSPTSE','^MXX','^BVSP');
$count = 0;
$quote = new getStock;
echo '<b>EQUITY INDEXES</b>'."\n";
echo '<table class="t" border="0">';
foreach ($stocks as $stock) {
    $quote->getQuote($stocks[$count++]);
    echo '<tr><td class="sym">'.$quote->symbol,':'.'</td>'."\n";
    echo '<td class="qte">' .$quote->price. ','."\n";
    echo '<span style="';
    if ($quote->change > 0) {
        echo 'color:#006600;">' .$quote->change. ',' .$quote->percent. '%';
    } elseif ($quote->change < 0) {
        echo 'color:#FF0000;">' .$quote->change. ',' .$quote->percent. '%';
    } else {
        echo 'font-size:100%;">' .$quote->change. ',' .$quote->percent. '%';
    }
    echo '</span></td></tr>'."\n";
}
echo '</table>'."\n";
?>
My question:is there a way to echo stock name instead of symbols by using replace function or such? this is what its echo at moment:
  • EQUITY INDEXES
    ^DJI: 8,378.95, -312.30,-3.6%
    ^IXIC: 1,552.03, -51.88,-3.2%
    ^GSPC: 876.77, -31.34,-3.5%
    ^GSPTSE: 9,294.09, 0.00,0.0%
    ^MXX: 16,978.84, -820.15,-4.6%
    ^BVSP: 31,481.55, -2,336.94,-6.9%
but I'm trying to make the script echo this:
  • EQUITY INDEXES
    DOW: 8,378.95, -312.30,-3.6%
    NAS: 1,552.03, -51.88,-3.2%
    ...
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Stock ticker help

Post by jaoudestudios »

Yes if you have a comparison table. i.e. which symbol has what full name.
novicephp
Forum Newbie
Posts: 2
Joined: Mon Oct 27, 2008 2:06 am

Re: Stock ticker help

Post by novicephp »

Thanks for the reply:
I'm new to PHP :| how can I write this table, I know how to use replace function but not comparison table
should I say ['^DJI']=['Dow'] and should I apply this before array or after.
Thanks for your help
Post Reply