codingcasually
-
Posts
9 -
Joined
-
Last visited
Posts posted by codingcasually
-
-
@quickoldcar: that is exactly how my non-functioning script looks like. I'm afraid that errant } in my code above was inadvertently carried over from my copying and pasting. Besides, that would have thrown off a syntax error.
Thanks though. -
@chou3r: thanks for the suggestions, I did previously added include, and require as well, the latter just so it would trigger a fatal error if in fact something was wrong. Still got a blank page. Likewise when I incorporated your suggested error reporting. Thanks for the tip though. Puzzles me how such a spartan script can still fail.
-
@mac_gyver: sorry, my bad, I didn't copy and paste that code, which I should have. It was so short that I decided to just type it right there. The original is tagged correctly, I would've noticed the syntax highlighting otherwise. It's still square 1 for me.
-
I'm writing a program that allows users to input a stock and view a historical price chart. Code is straight out of libchart charting library. Problem is, user is supposed to enter the stock symbol from a form handler (index.php) which then passes the symbol as a variable to the charting function, which doesn't get called at all:
<? php
function index() { chart($_POST['userinput']};
} ?> .
<?php
function chart($stock)
{
$prices=getdata($stock); //returns array of prices from yahoo finance
$data=analyzer($prices); //produces metrics to be charted
graph($data); } ?> //plots the metrics, outputs .html
chart.php works on its own, as I've verified by hardcoding $argv='ibm'; in its code body. But index.php can't seem to call chart.php, as I've verified by including an echo/var_dump line in chart.php (doesn't get executed at all). It doesn't have anything to do with form handling either, as chart('ibm'); doesn't call chart.php either. I don't understand how a 6-line piece of code can be so problematic. All files are in the same folder, btw.
Thanks.
-
@barand: Dude, you know what, I made a dumb mistake, I did a print_r previously but didn't scroll all the way down to see all 43 entries. I followed your advice with var_export (output the same array) but this time I noticed my final was messed up, I tacked on a metric I calculated that resulted in the error.
Thanks for your help!
-
@quickoldcar: My mistake, I missed a ) transcribing to my post, the original did have it, php would've caught it otherwise. Thanks.
@barand: the first part of the code pulls 2-month stock price data from yahoo finance csv, and populates date and open, high, low, close price keys correspondingly (above snippet uses close). Here's gist of (not very cleanly-written) code:
$url = 'http://real-chart.finance.yahoo.com/table.csv?s='.'KO'.'&a='.$prevmonth.'&b='.$prevday.'&c='.$prevyear.'&d='.$curmonth.'&e='.$curday.'&f='.$curyear.'&g=d&ignore=.csv'; //'KO' refers to ticker symbol of Coca Cola
$handle = fopen($url, "r");
$arr['filesize'] = sizeof($handle);
$keys = array('line', 'date', 'open', 'high', 'low', 'close', 'volume', 'adj');
//new keys
$arr = array();
$buffer = fgets($handle, 5120);
if ($buffer == "")
return false;
//parse each line and place into array $one
while (($buffer = fgets($handle, 5120)) !== false) {
if (!preg_match('/([0-9\-]{10})[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)[\,]{1}([0-9\.]+)\s?/', $buffer, $one))
return false;
$one = array_combine($keys, array_values($one)); //rekeying
$rawdata[] = $one;
}if (!feof($handle))
{
echo "Error: unexpected fgets() fail\n";
return false;
}
fclose($handle);
for ($i=0; $i<sizeof($rawdata); $i++)
{
$dataarray[$i]['date']=$rawdata[$i]['date'];
$dataarray[$i]['high']=$rawdata[$i]['high'];
$dataarray[$i]['low']=$rawdata[$i]['low'];
$dataarray[$i]['close']=$rawdata[$i]['close'];
}
$prices = (array_reverse($dataarray));//flip price array orderthe charting code comes straight from libchart documentation, which plots daily closing prices (Y axis) and trading date (X axis):
$chart1 = new LineChart();//start macdchart
$serie1 = new XYDataSet();
$chart2 = new LineChart();
$serie2 = new XYDataSet();
$serie3 = new XYDataSet();
for ($i=25; $i<sizeof($prices); $i++)
{
$serie1->addPoint(new Point($prices[$i]['date'], $prices[$i]['close']);
}Hope this is enough detail. Thanks.
-
@mogosselin: Yes, it is an array as verified by is_array(), plus I've used the price key in some functions in the same code prior to the 1 specified above without any problems. Je n'ai aucune idee, pourquoi il ne marche pas. Merci beaucoup.
@daviddannis: The indices are correct, and I have previously used print_r as well as isset() to verify that the array values are set and correct. Thanks for the suggestion though.
-
I am writing a script that downloads stock data and graphs the prices using libchart charting library. After downloading data to array $prices, I use the following line to add a new data point to my chart:
$serie1->addPoint(new Point($prices[$i]['date'], $prices[$i]['price']);
$prices contains both date and price keys. However, this throws off illegal string offset error for both keys. I have checked with isset, both variables contain values. I have also tried typecasting, date with (string) and price with (float), but these threw off new errors.
What could be causing the illegal string offset error? Is there a way to silence it?
can't call function
in PHP Coding Help
Posted
Finally, it's working! I had include 'chart.php', which wouldn't trigger an error warning. Thanks to @notioncommotion's suggestion, I used require instead, plus absolute, not relative path. Not sure why using absolute path would've made a difference, but it did. Thanks guys, this had been driving me crazy for a week.