ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Stock ticker. PHP in channel entries throwing errors.

June 14, 2012 1:28pm

Subscribe [1]
  • #1 / Jun 14, 2012 1:28pm

    liquiduscreative

    13 posts

    I’m not sure if this is even possible but I’m simply trying to pass a channel field into a php variable. My client needs to be able to add new stock symbols via a channel field. Any help/suggestions would be appreciated. This current code does “work” but it’s throwing php errors. I don’t know if creating a snippet or embedding would work better?

    {exp:channel:entries channel="stock_symbols" limit="1"}
    <?php
    echo "<ul class=\"ticker\">";
    $up = "{image_path}/up.png";
    $down = "{image_path}/down.png";
    
     // Original script by Walter Heitman Jr, first published on <a href="http://techblog.shanock.com">http://techblog.shanock.com</a>
    
     // List your stocks here, separated by commas, no spaces, in the order you want them displayed:
     $stocks = "{symbols}";
    
     // Function to copy a stock quote CSV from Yahoo to the local cache. CSV contains symbol, price, and change
     function upsfile($stock) { copy("http://finance.yahoo.com/d/quotes.csv?s=$stock&f=sl1d1t1c1ohgv&e=.csv","stockcache/".$stock.".csv"); }
    
     foreach ( explode(",", $stocks) as $stock ) {
    
      // Where the stock quote info file should be…
      $local_file = "stockcache/".$stock.".csv";
    
      // ...if it exists. If not, download it.
      if (!file_exists($local_file)) { upsfile($stock); }
      // Else,If it's out-of-date by 15 mins (900 seconds) or more, update it.
      elseif (filemtime($local_file) <= (time() - 900)) { upsfile($stock); }
    
      // Open the file, load our values into an array…
      $local_file = fopen 〈"stockcache/".$stock.".csv","r");
      $stock_info = fgetcsv ($local_file, 1000, ",");
    
      // ...format, and output them. I made the symbols into links to Yahoo's stock pages.
      
      echo "<li class=\"tick-text\"><span>".$stock_info[0]." </span>".sprintf("%.2f",$stock_info[1])." <span>=0) { echo "color: #62a800;\">$up "; }
      elseif ($stock_info[2]<0〉 { echo "color: #ff0000;\">$down "; }
      echo sprintf("%.2f",abs($stock_info[2]))."</span></span></li>\n";
      // Done!
      fclose($local_file); 
     }
    echo "</ul>";
    ?>
    {/exp:channel:entries}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases