I'm very new to PHP and trying to self learn, I know html, but PHP is very new to me.
Script: Yahoo! Stock Quote Retriever
The script is from: http://programmabilities.com/php/?id=24
The problem or fix:
I have the above file and it it's working fine, the only problem is that it pulls in the stock quote short cut, and not the full name of the stock.
e.g
^FTSE
Last Trade: $6795.32
Change: -111.74
When I want it to read like:
FTSE 100 ^FTSE
Last Trade: $4795.32 Change: -111.74
Thanks in advance
CODE:
<?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 ... hgv&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('^FTSE','^FTMC','^GDAXI','^FCHI');
$i = 0;
// Declare class.
$quote = new yahoo; // new stock.
// Loop thru array of symbols.
echo "<ul>\n";
foreach ($symbols as $symbol) {
$quote->get_stock_quote($symbols[$i++]); // Pass the Company's symbol.
echo "<li><a href=\"http://finance.yahoo.com/q?s=" .$symbol. "\" title=\"Yahoo! Finance: " .$symbol. "\">$quote->symbol</a>\n"; // can use $quote->symbol or $symbol
echo "<ul>\n";
echo "<li>Last Trade: \$" .$quote->last. "</li>\n"; // price.
echo '<li>Change: <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";
echo "</ul>\n";
echo "</li>\n";
} // endforeach
echo "</ul>\n";
?>
How do I add a name in front of an array
Moderator: General Moderators
-
monkeymagic777
- Forum Newbie
- Posts: 2
- Joined: Sun Jun 08, 2008 9:53 am
Re: How do I add a name in front of an array
You could do something like this:
I created an associative array called $full_names which assigned a new name for each $symbol. Then wherever you need to use the full name instead of the symbol just use $full_name[$symbol].
Hope that helps
Code: Select all
// Stock symbols.
$symbols = array('^FTSE','^FTMC','^GDAXI','^FCHI');
$full_names = array('^FTSE' => 'FTSE 100 ^FTSE', '^FTMC' => 'FTMC 100 ^FTMC', '^GDAXI' => 'GDAXI' 100 ^GDAXI'', '^FCHI' => 'FCHI 100 ^FCHI'); //Change the full name designations to whatever you need e.g. 'FCHI 100 ^FCHI' to whatever the proper designation is
$i = 0;
// Declare class.
$quote = new yahoo; // new stock.
// Loop thru array of symbols.
echo "<ul>\n";
foreach ($symbols as $symbol) {
$quote->get_stock_quote($symbols[$i++]); // Pass the Company's symbol.
echo "<li><a href=\"http://finance.yahoo.com/q?s=" .$symbol.. "\" title=\"Yahoo! Finance: " .$symbol. "\">$full_names[$symbol]</a>\n"; // can use $quote->symbol or $symbol
echo "<ul>\n";
echo "<li>Last Trade: \$" .$quote->last. "</li>\n"; // price.
echo '<li>Change: <span style="';
/* Make the + or - change elicit differing coloration. */
$str = $quote->change;
Hope that helps
-
monkeymagic777
- Forum Newbie
- Posts: 2
- Joined: Sun Jun 08, 2008 9:53 am
Re: How do I add a name in front of an array
PERFECT
Just what I needed and what I could not figure out myself, and a big thanks for the fast reply.
Just what I needed and what I could not figure out myself, and a big thanks for the fast reply.