Stock Ticker

Post any questions you have about PHP coding and it doesn't have to be about the login script :)

Stock Ticker

Postby bigbinc on Mon Jun 09, 2008 4:04 pm

Hi, Is there a way I could put the output of this into a scrolling ticker?
BTW, I really like your login script I searched the internet for hours trying to find one that would fit my needs, and yours is by far the best. Thanks.


Code: Select all
<?php
/* Class. */
Class yahoo
{
    /* Function. */
    function get_stock_quote($symbol)
    {
        // Yahoo! Finance URL to fetch the CSV data.
        $url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=$symbol&f=sl1d1t1c1ohgv&e=.csv", $symbol);
        $fp  = fopen($url, 'r');
        if (!fp) {
            echo 'Error : cannot recieve stock quote data.';
        } else {
            $data = @fgetcsv($fp, 4096, ', ');
            fclose($fp);
            $this->symbol = $data[0]; // Stock symbol.
            //$this->last   = $data[1]; // Last Trade (current price).
            //$this->date   = $data[2];
            //$this->time   = $data[3];
            $this->change = $data[4]; // + or - amount change.
            //$this->open   = $data[5];
            //$this->high   = $data[6];
            //$this->low    = $data[7];
            //$this->volume = $data[8];
        }
    }
}

// Stock symbols.
$symbols = array('^DJI','^IXIC','^GSPC', '^N225','^FTSE');
$i = 0;
// Declare class.
$quote = new yahoo; // new stock.
// Loop thru array of symbols.
foreach ($symbols as $symbol) {
    $quote->get_stock_quote($symbols[$i++]); // Pass the Company's symbol.
    echo $quote->symbol; // can use $quote->symbol or $symbom
    echo ' <span style="';
    /* Make the + or - change elicit differing coloration. */
    $str   = $quote->change;
    $first = $str{0};
    if ($first == '+') {       // If we gained print the # in GREEN.
        echo 'color:#009900;';
    } elseif ($first == '-') { // If we lost RED.
        echo 'color:#990000;';
    } else {                   // NO color.
        echo 'font-weight:normal;';
    } // endelseif
    echo "\">" .$quote->change. "</span></li>\n";
} // endforeach
?>
bigbinc
New
 
Posts: 6
Joined: Mon Jun 09, 2008 3:12 pm

Re: Stock Ticker

Postby Josh on Mon Jun 09, 2008 9:09 pm

First off, welcome to the forum bigbinc!

Just one question before I begin, why did you include a closing </li> tag and no opening one?

I'm sure it was just a simple mistake, but could you please clarify?

Also, did you want it to scroll horizontally or vertically?
Josh
Site Admin
 
Posts: 476
Joined: Mon Nov 05, 2007 7:30 pm
Location: California

Re: Stock Ticker

Postby bigbinc on Tue Jun 10, 2008 12:45 am

Hi, Thanks, :D
Yes, the </li> is a mistake, I had something else there and must have forgot to remove it, I would like it to scroll horizontally, and if possible I would like it to display the full name of the stock instead of the symbol, like "^DJI" would be "Dow Jones" and, "^IXIC" would be "Nasdaq", and "^GSPC" would be "S&P 500" and "^N225" would be Nikkei and "^FTSE" would be "FTSE 100". I don't know much about programming, I tried to do it myself and spent many hours making mistakes, Thank you very much.
bigbinc
New
 
Posts: 6
Joined: Mon Jun 09, 2008 3:12 pm

Re: Stock Ticker

Postby Josh on Wed Jun 11, 2008 8:48 pm

I attached the final product. Hope you like it! :D

If you want to add more symbols, add them to the arrays. :)

Oh, and thanks to mioplanet for the scrolling ticker! Didn't write one myself mainly because I was, am, err,..trying to be resourceful and efficient. :P
Attachments
stock_ticker.rar
Stock Ticker
(1.75 KiB) Downloaded 21 times
Josh
Site Admin
 
Posts: 476
Joined: Mon Nov 05, 2007 7:30 pm
Location: California

Re: Stock Ticker

Postby bigbinc on Thu Jun 12, 2008 5:01 pm

Thanks!! Could I ask for one more thing? Instead of just the numbers changing color could the stock names change color as well. I tried to do this but I'm too stupid. :cry:
bigbinc
New
 
Posts: 6
Joined: Mon Jun 09, 2008 3:12 pm

Re: Stock Ticker

Postby Josh on Thu Jun 12, 2008 5:20 pm

Aww, don't say that about yourself. :P

Oh, sure thing the names can change color as well as the numbers! :)

I have attached the new and improved script. Download below! :D

Hope it all works for ya'!
Attachments
stock_ticker.rar
New and improved Stock Ticker!!!
(1.85 KiB) Downloaded 24 times
Josh
Site Admin
 
Posts: 476
Joined: Mon Nov 05, 2007 7:30 pm
Location: California

Re: Stock Ticker

Postby bigbinc on Sun Jun 22, 2008 6:42 am

Thanks, works great! :D
bigbinc
New
 
Posts: 6
Joined: Mon Jun 09, 2008 3:12 pm

Re: Stock Ticker

Postby Josh on Wed Jun 25, 2008 10:04 pm

You're welcome and we (Doug and I) hope your time as a developer is successful! :D

If you need any other help or advice, you are more than welcome to post.
Josh
Site Admin
 
Posts: 476
Joined: Mon Nov 05, 2007 7:30 pm
Location: California


Return to PHP Coding

Who is online

Users browsing this forum: No registered users and 0 guests

cron