How do I add a name in front of an array
Posted: Sun Jun 08, 2008 10:08 am
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";
?>
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";
?>