mysql - undefined variable and index in PHP -
i'm getting undefined variable , undefined index notices thought isset() should have done away with. in example i'm reading through db consisting of individuals names , companies. want separate out unique companies, know how many times represented. here's i'm expecting do:
- open database, count entries, load first row table.
- the first 'if' inside 'for' loop looks see if $comp variable hash value of company name exists. if does, increment count
- if $comp variable not set, first time i've seen company go 'else'.
however, isset() not seem working because php still throwing me notices line of code , variable , indexes aren't defined.
$r1q = mysql_query("select * totals", $db); $r1c = mysql_num_rows($r1q); $r1 = mysql_fetch_array($r1q); $title = "companies represented in db"; printf(" <div class=\"content\"> <h2>%s</h2> <center> <table border=1 bgcolor=#ffffff> <tr bgcolor=#cc9933> <th width=150>company</th> <th width=50>times</th> </tr> ", $title); // loop through 1 time each entry in db for($j=0;$j<$r1c;$j++) { $company = $r1[4]; // have seen company before? if(isset($comp[$company])) { $cnt[$company]++; $cnt1[$company]++; } else { $comp[$company] = $r1[4]; $cnt[$company] = 1; $cnt1[$company] = 1; }
you should initialize arrays either:
add before loop:
$comp = array(); $cnt = array(); $cnt1 = array();
Comments
Post a Comment