Author Topic: stock ticker help?  (Read 358 times)

0 Members and 1 Guest are viewing this topic.

Offline BelleanmeTopic starter

  • Irregular
  • Posts: 4
    • View Profile
stock ticker help?
« on: June 10, 2010, 05:42:03 PM »
A client requested a stock ticker on his site.  I found this code, which works fine:
<?php
         ///// Replace with Yahoo Finance stock quotes of your choice //////
         $raw_stock_data = array('AAPL 200 205',
         'BIDU 500 510','RIMM 150 155', 'SNDK 70 72',
         'GOOG 500 510','MSFT 30 32','AMD 9 10');
         sort($raw_stock_data);
         //////////////////////////////////////////////////////////////////////////
?>
<div align="center">
            <marquee bgcolor="#e0e1e1" direction="left" loop="20" width="100%%">
<?php
                  foreach ($raw_stock_data as $value) {
               $results = explode(' ',$value);
               
               $ticker = $results[0];
               $buy = $results[1];
               $sell = $results[2];
               
               echo "<font color=\"#ffffff\">$ticker </font>";
               echo "<font color=\"#00ff00\">$buy </font>";
               echo "<font color=\"#ff0000\">$sell </font>";
               echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
               }
?>
Now, I need to get it to feed live data from the free Yahoo stock info online.  I looked for days and finally found this code:
<?php
// Code by:   Don Wilson
// Edited:     October 24, 2003
class stock
{
   function getLastTrade($symbol)
   {
      $file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");
         $read = fread($file, 2000);
         fclose($file);
      $read = str_replace(""", "", $read);
      $read = explode(",", $read);
      return $read[1];
   }

   function getChange($symbol)
   {
      $file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");
         $read = fread($file, 2000);
         fclose($file);
      $read = str_replace(""", "", $read);
      $read = explode(",", $read);
      return $read[4];
   }

   function getDateClosed($symbol)
   {
      $file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");
         $read = fread($file, 2000);
         fclose($file);
      $read = str_replace(""", "", $read);
      $read = explode(",", $read);
      return $read[2] . " " . $read[3];
   }

}

$stock = new Stock();

echo "<pre>\n";
if(isset($symbols))
{
   $symbols = explode(",", $symbols);
   foreach($symbols as $symbol)
   {
   $change = $stock->getChange($symbol);
   if($change < 0)
     $change = "<font color=red>{$change}</font>";
   elseif($change > 0)
     $change = "<font color=green>{$change}</font>";
   else
     $change = "0.00";
    
      echo "Symbol:         " . $symbol                              . "\n";
      echo "Last Trade:      " . $stock->getLastTrade($symbol)      . "\n";
      echo "Change:         " . $change         . "\n";
      echo "Date Closed:      " . $stock->getDateClosed($symbol)   . "\n";
      echo "\n";
   }
}
echo "</pre>\n";
?>
...that a nice person kindly shared.  I need to integrate the two.  What the client wants is just basic stocks, (the ones there listed are fine) and the high and low amount for the day.  Can someone please help?   :D  I'd be so appreciative!
« Last Edit: June 10, 2010, 05:50:49 PM by Belleanme »

Offline BelleanmeTopic starter

  • Irregular
  • Posts: 4
    • View Profile
corrected first post
« Reply #1 on: June 10, 2010, 05:46:16 PM »
I am trying to delete this second entry...
« Last Edit: June 10, 2010, 05:53:01 PM by Belleanme »