0

I'm receiving the following error in php: Notice: Undefined index: panel_num.

I think I need to use isset() but I can't seem to get it to work with while

global $d;
$i = 1;
while($i <= $d['panel_num']){
$options[] = array(
                      "name" => "Panel".$i,
                        "id" => "panel_".$i,
                       "std" => "",
                      "type" => "panel");
$i++;
}

What is the proper way to resolve this issue?

teamcrisis
  • 709
  • 1
  • 6
  • 14
  • The array `$d` obviously doesn't contain what you expect it to. `print_r($d)` to see what's actually in there, and if the `panel_num` key doesn't exist, either bail out of the operation or use a default value instead. – Michael Berkowski Jun 13 '13 at 14:00
  • 1
    What's in `$d`? do a `var_dump($d)` and see what it looks like. – andrewsi Jun 13 '13 at 14:00

2 Answers2

1

Check to see if that variable is set before using it:

global $d;
if (isset($d['panel_num']))
{
  $i = 1;
  while($i <= $d['panel_num']){
  $options[] = array(
                      "name" => "Panel".$i,
                        "id" => "panel_".$i,
                       "std" => "",
                      "type" => "panel");
  $i++;
  }
}
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

I think you just need to check for isset() and not empty $d['panel_num']

global $d;
if(isset($d['panel_num']) && !empty($d['panel_num']))
{
    $i = 1;
    while($i <= $d['panel_num']){
    $options[] = array(
                  "name" => "Panel".$i,
                    "id" => "panel_".$i,
                   "std" => "",
                  "type" => "panel");
    $i++;
    }
}

So you will avoid to call your variable if it is not set or it's empty

Fabio
  • 23,183
  • 12
  • 55
  • 64