Undefined index error

I have 40+ of these type of lines on my page, but I’m getting an undefined index error on a odd one here and there, why are lines like below being singled out from the others.

echo "<td width='60' class='tableData'>".$data["Region"]."</td>";

Notice: Undefined index: Region in \CSFFILES11\WEBSITES\dev\csfintranet\advanced_Search.php on line 1147

This is another one, but have picked it out as its different.

if ($data["Group_Member"]==1){

This is not much to go on.
Where is the $data array getting its data from?

Is there an actual problem with the final page output, as in, there is content missing as a result of the error?

Hi SamA74, these are only showing when I have errors on, otherwise it works fine, but just don’t understand why some are singled out whilst others the same aren’t.

I can easily resolve these below, which I have done now, but I though isset was more to use when using get or post

if (isset($data["Group_Member"])==1){

But the others are strange

That would depend on the data source.

If there is no problem with content missing from the script output, the errors could be removed by adding an isset clause to only attempt to echo indexes that have been defined.

function outputIndex($array,$index) {
   if(isset($array[$index])) {
      echo htmlspecialchars($array[$index]);
      return true;
   }
   else{ return false; }
}

You can the use the function to otput data where you want it.

outputIndex($data,'Region');
1 Like

Thank you, have never come across that before and it worked perfectly.

Just be a bit careful with these sorts of work arounds. The underlying problem is that $data does not contain everything it should. What if some other part of the code relies on the data being set?

Might consider initializing $data to at least have default values for each index. Better yet, track down why data is missing. Eventually it will bite you.

That’s what I’m getting at here.
It may be that, for example the data is getting pulled from a database where there may be fields with empty or null values, that leave empty cells in the resulting html table. That may be normal and expected. But if you are expecting to see some data for everything, there is a problem that needs further investigation.
Since we have no idea where the $data array is coming from, it’s impossible to say.

Thanks guys, I will go back and see whats causing the problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.