Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Undefined index in WP_DEBUG WordPress

  • SOLVED

I had a similar issue yesterday, but this one is a bit different.

I'm getting undefined index for a variety of variables.

Here's the code...

The error message i receive relates each incident to lines 3, 4 and 5 below. I've added the rest of the code so you can see how this is used.

Thank you for your help.



case 'border':
$default = $value['std'];
$border_stored = array('width' => $data[$value['id'] . '_width'] ,
'style' => $data[$value['id'] . '_style'],
'color' => $data[$value['id'] . '_color'],
);

/* Border Width */
$border_stored = $data[$value['id']];

$output .= '<select class="of-border of-border-width" name="'.$value['id'].'[width]" id="'. $value['id'].'_width">';
for ($i = 0; $i < 21; $i++){
$output .= '<option value="'. $i .'" ' . selected($border_stored['width'], $i, false) . '>'. $i .'</option>'; }
$output .= '</select>';

/* Border Style */
$output .= '<select class="of-border of-border-style" name="'.$value['id'].'[style]" id="'. $value['id'].'_style">';

$styles = array('none'=>'None',
'solid'=>'Solid',
'dashed'=>'Dashed',
'dotted'=>'Dotted');

foreach ($styles as $i=>$style){
$output .= '<option value="'. $i .'" ' . selected($border_stored['style'], $i, false) . '>'. $style .'</option>';
}

$output .= '</select>';

/* Border Color */
$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color: '.$border_stored['color'].'"></div></div>';
$output .= '<input class="of-color of-border of-border-color" name="'.$value['id'].'[color]" id="'. $value['id'] .'_color" type="text" value="'. $border_stored['color'] .'" />';

Answers (4)

2011-04-25

AdamGold answers:

Your arrays doesn't include the indexes they are expected to include, so you need to do something like this:

case 'border':
if ( isset($value['std']) && isset($value['id']) ) {
$default = $value['std'];
$border_stored = array('width' => $data[$value['id'] . '_width'] ,

'style' => $data[$value['id'] . '_style'],

'color' => $data[$value['id'] . '_color'],

);

}

/* Border Width */
$border_stored = $data[$value['id']];


$output .= '<select class="of-border of-border-width" name="'.$value['id'].'[width]" id="'. $value['id'].'_width">';

for ($i = 0; $i < 21; $i++){

$output .= '<option value="'. $i .'" ' . selected($border_stored['width'], $i, false) . '>'. $i .'</option>'; }

$output .= '</select>';



/* Border Style */

$output .= '<select class="of-border of-border-style" name="'.$value['id'].'[style]" id="'. $value['id'].'_style">';



$styles = array('none'=>'None',

'solid'=>'Solid',

'dashed'=>'Dashed',

'dotted'=>'Dotted');



foreach ($styles as $i=>$style){

$output .= '<option value="'. $i .'" ' . selected($border_stored['style'], $i, false) . '>'. $style .'</option>';

}



$output .= '</select>';



/* Border Color */

$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color: '.$border_stored['color'].'"></div></div>';

$output .= '<input class="of-color of-border of-border-color" name="'.$value['id'].'[color]" id="'. $value['id'] .'_color" type="text" value="'. $border_stored['color'] .'" />';
}


Armand Morin comments:

That worked perfectly... THANKS.

2011-04-25

Jarret Minkler answers:

What errors are you getting? The arrays don't have these values as keys. You need to step backwards to where these arrays are getting set and see what's wrong.

I like to use FirePHP here and just dump out vars

fb($site, 'site array'); etc

If you coding in PHP without FirePHP, you're doing it wrong.

2011-04-25

Hameedullah Khan answers:

As you are using the same index to get values from your array in almost all of your code. So, the best bet is to assign an empty values to the indexes that doesn't exist at the beginning of your code.

So your above code will look like:

case 'border':
if (!array_key_exists('std', $value)) $value['std'] = '';
if (!array_key_exists('id', $value)) $value['id'] = '';

$default = $value['std'];

$border_stored = array('width' => $data[$value['id'] . '_width'] ,

'style' => $data[$value['id'] . '_style'],

'color' => $data[$value['id'] . '_color'],

);



/* Border Width */

$border_stored = $data[$value['id']];



$output .= '<select class="of-border of-border-width" name="'.$value['id'].'[width]" id="'. $value['id'].'_width">';

for ($i = 0; $i < 21; $i++){

$output .= '<option value="'. $i .'" ' . selected($border_stored['width'], $i, false) . '>'. $i .'</option>'; }

$output .= '</select>';



/* Border Style */

$output .= '<select class="of-border of-border-style" name="'.$value['id'].'[style]" id="'. $value['id'].'_style">';



$styles = array('none'=>'None',

'solid'=>'Solid',

'dashed'=>'Dashed',

'dotted'=>'Dotted');



foreach ($styles as $i=>$style){

$output .= '<option value="'. $i .'" ' . selected($border_stored['style'], $i, false) . '>'. $style .'</option>';

}



$output .= '</select>';



/* Border Color */

$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color: '.$border_stored['color'].'"></div></div>';

$output .= '<input class="of-color of-border of-border-color" name="'.$value['id'].'[color]" id="'. $value['id'] .'_color" type="text" value="'. $border_stored['color'] .'" />';


2011-04-25

Vidyut Kale answers:

basically, you have to check if the variable has been set. It will only show in debug mode, as usually you'll be calling to retreive something that you know exists.

Basically, where you get this error, just check if it is set before calling it.


if(isset($value['std'])) $default = $value['std'];



lather, rinse, repeat