Notice: undefined offset???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Notice: undefined offset???

Post 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]
}
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

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

Post 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. :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

Thanks!

Post by afbase »

i think i'll try playing with 'break' and 'continue'. thanks for the link 8)
Post Reply