0

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:

  1. Open the database, count the entries, load up the first row from the table.
  2. 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
  3. 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;
        }
3
  • "that line of code" is... what? For that matter what is the text of the error message? Commented Mar 24, 2014 at 21:09
  • I don't see where you create the $comp array. Is there more code above this? Commented Mar 24, 2014 at 21:12
  • Sorry, "that line of code" is the line with the if(isset()). If you see my comments on other posts below, I've now defined the arrays, but I still get notices about Undefined variables. If I just simply turn off the notices (error_reporting(E_ALL ^ E_NOTICE);) the page prints fine. Commented Mar 25, 2014 at 13:29

4 Answers 4

2

You should initialize the arrays either:

Add this before the loop:

 $comp = array();
 $cnt = array();
 $cnt1 = array();
Sign up to request clarification or add additional context in comments.

1 Comment

Good improvement, but not the problem.
0

The issue I believe is not your isset() function, but the line before that where you reference [4] of $r1.

for($j=0;$j<$r1C;$j++) {
   $company = $r1[4];

I think this index may not exist, since isset should work just fine in the following statement.

for($j=0;$j<$r1C;$j++) {
   if(isset($r1[4])) {
     $company = $r1[4];
   }else{
     // Handle Error
   }
   // Rest of your for statement
}

Without seeing all of the code and what line the error happens on, this is a lot more difficult to narrow down. Many errors occur where users often forget to look, or post onto the site.

2 Comments

Here is an example of the first two errors: Notice: Undefined variable: seen in C:\xampp\htdocs\xampp\companySearch.php on line 781 Notice: Undefined index: IBM in C:\xampp\htdocs\xampp\companySearch.php on line 781 Line 781 refers to my if(isset()) line of code above. I have verified that the $company =$r1[4] is not indeed NULL, it has a value.
I've followed the previously suggested code updates. I've defined the variables as arrays and I've added the && $comp[$company] != false. I still get the Undefined variable message. It is worth noting too that as soon as I set error_reporting(E_ALL ^ E_NOTICE); the page prints out the generated table of companies perfectly.
0

Simple edit to your if statement:

if(isset($comp[$company]) && $comp[$company] != false) { // edit was made here
            $cnt[$company]++;
            $cnt1[$company]++;
}

Because the key may exist but it may not have a value, so what will it increment from? It won't just assume 0, since it could also be another type. Any number other than 0 will be equal to false in PHP, so you can add the additional comparison above to fix your problem.

Comments

-1

You need to specify that the variable is indeed an array in PHP.

Use this example for the docs linked below:

$firstquarter = array('January', 'February', 'March');

PHP array docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.