I'm getting an undefined variable and undefined index notices that I thought isset() should have done away with. In this example I'm reading through a db consisting of individuals names and their companies. I want to separate out the unique companies, so I know how many times they are represented. Here's what I'm expecting it to do:
- Open the database, count the entries, load up the first row from the table.
- The first 'if' inside the 'for' loop looks to see if the $comp variable with a hash value of the company name exists. If it does, increment the count
- If $comp variable is not set, this is the first time I've seen this company so go to the 'else'.
However, isset() does not seem to be working because PHP is still throwing me notices about that line of code and that my variable and indexes aren't defined.
$r1Q = mysql_query("SELECT * FROM totals", $db);
$r1C = mysql_num_rows($r1Q);
$r1 = mysql_fetch_array($r1Q);
$title = "Companies Most 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 for each entry in the db
for($j=0;$j<$r1C;$j++) {
$company = $r1[4];
// have i seen this company before?
if(isset($comp[$company])) {
$cnt[$company]++;
$cnt1[$company]++;
}
else {
$comp[$company] = $r1[4];
$cnt[$company] = 1;
$cnt1[$company] = 1;
}