Page 1 of 1

Notice: undefined offset???

Posted: Sat Dec 30, 2006 3:20 pm
by afbase
I noticed that when I curl information off webpages on stocks, they will simply have no data recorded. When a stock has nothing to yield in data it returns this notice:
Notice: Undefined offset: 1 in /home/clinton/hdd1/coldowl/project/stock/tests/stock_class.php on line 287
a good example of this would be ticker symbol, ARC1A. I typically get the notice when the website it self doesn't recognize the ticker as a valid stock/bond/security. Could someone explain what is this notice telling me exactly and how do I correct it?

Code: Select all

<?php
class example{
var $dividend;
function mysql_dividend($ticker){
        $url = "http://www.zacks.com/research/report.php?type=detailed&t=".$ticker;
        $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $result = curl_exec($ch);
                curl_close($ch);
        $pattern='!Dividend</td>\s*<td\s*align=right\s*width=50%\s*class="ticker">([^<]*)</td>!';
        preg_match($pattern,$result,$this->dividend);
        echo $this->dividend[1]
}
}
?>

Posted: Sat Dec 30, 2006 3:31 pm
by volka
Why has the function the name mysql_dividend? It has nothing to do with mysql.
preg_match returns 0 if there is no match. Therefore we can use preg_match(...) or die(...) as emergency brake

Code: Select all

<?php
class example{
	var $dividend;
	function mysql_dividend($ticker){
        $url = "http://www.zacks.com/research/report.php?type=detailed&t=".$ticker;
        $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $result = curl_exec($ch);
                curl_close($ch);
        $pattern='!Dividend</td>\s*<td\s*align=right\s*width=50%\s*class="ticker">([^<]*)</td>!';
        preg_match($pattern,$result,$this->dividend) or die('pattern does not match');
        echo $this->dividend[1];
	}
}

$c = new example;
$c->mysql_dividend('ARC1A');
?>
and it says
pattern does not match
afbase wrote:$pattern='!Dividend</td>\s*<td\s*align=right\s*width=50%\s*class="ticker">([^<]*)</td>!'
e.g. I can't find any class="ticker" when adding a echo $result.

edit and btw: viewtopic.php?t=61342
volka wrote:At least add error handling for the case preg_match fails/the pattern does not match.

die () isn't good news is there an alternative

Posted: Sat Dec 30, 2006 3:45 pm
by afbase
the die () will kill my looping script that uses this method, is there an alternative solution other than die (i.e. do_not_die())?


ya i'm not very good at naming my methods. originally when i concocted all of this in my head i had planned for this method to have mysql stuff in it but it turns out it didn't. :)

Posted: Sat Dec 30, 2006 4:09 pm
by volka
afbase wrote:is there an alternative solution other than die (i.e. do_not_die())?
see http://de2.php.net/manual/en/language.c ... ctures.php

Thanks!

Posted: Sat Dec 30, 2006 4:27 pm
by afbase
i think i'll try playing with 'break' and 'continue'. thanks for the link 8)