Welcome Guest, Not a member yet? Register   Sign In
"Undefined index" error, yet the variable is fine
#1

[eluser]Wayne Smallman[/eluser]
Hi guys!

I'm getting an undefined index error for a variable which is perfectly fine.

I'm passing an associative array variable to a function, whereupon I use the variable in a switch function.

However, while the variable works fine without error in a similar function (and in several other similar functions elsewhere), this one function simply will not stop generating errors, no matter what I do.

Here's the code — this is where I call the function:

Code:
$array_options['options']['show'] = $uri_segment['show'];

$this->variable = $this->data_relationships->select_records(array(), $array_options);
And this is the function in question:
Code:
function select_records ($array_data, $array_options) {
...
    switch($array_options['options']['show']):

        case 'mine':
            $sql .= " ... ";            
        break;

        case 'friends':
            $sql .= " ... ";
        break;

        case 'all':
        default:
            $sql .= " ... ";
        break;

    endswitch;
The error is on the switch function itself, where the variable is being used.

Any ideas why this is happening here but nowhere else?
#2

[eluser]Cristian Gilè[/eluser]
I can't see any error. Could you post more code?


Cristian Gilè
#3

[eluser]cideveloper[/eluser]
I just tested your code and it worked fine.

What is the error exactly that shows on the screen?

Are you redefining $array_options in your model.

you have .....above the switch. Anything in there.
#4

[eluser]Wayne Smallman[/eluser]
[quote author="progr@mmer" date="1295074952"]I just tested your code and it worked fine.

What is the error exactly that shows on the screen?

Are you redefining $array_options in your model.

you have .....above the switch. Anything in there.[/quote]

Hi and thanks for the reply!

Here's the error code in full:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: options

Filename: models/data_relationships.php

Line Number: 108
Line 108 is where the switch function is being called.

I've added an echo right up before the switch function and the value is perfecrtly valid and exactly what it should be.

Also, this error appears several times, as it's a query that's being called several times (I'll need to optimize the query at some point, but that's not important right now).

[quote author="Cristian Gilè" date="1295071090"]I can't see any error. Could you post more code?

Cristian Gilè[/quote]
Hi Cristian, and thanks for the reply!

There's nothing else I can provide that would be of any use; the code I provided is the exact point of failure.

As I explained above, there's nothing inhibiting or changing the value of the variable.

I think I'm going to swap out the switch function for an if statement, which includes an isset function, to quell the error messages.
#5

[eluser]Wayne Smallman[/eluser]
Hi guys!

I've re-written the code and it now works. But as you can see, it lacks the elegance of the switch function:

Code:
if (isset($array_options['options']['show'])):

    if ($array_options['options']['show'] == 'all'):

        $sql .= " ... ";

    elseif ($array_options['options']['show'] == 'mine'):

        $sql .= " ... ";            

    elseif ($array_options['options']['show'] == 'friends'):

        $sql .= " ... ";

    endif;

else: // the default query, which is 'all'

    $sql .= " ... ";

endif;
#6

[eluser]cideveloper[/eluser]
you can still use the switch, you just need to set a variable at the top

Code:
$aos = isset($array_options['options']['show']) ? $array_options['options']['show'] : 'all';
switch($aos):

case 'mine':
$sql .= " a ";            
break;

case 'friends':
$sql .= " b ";
break;

case 'all':
default:
$sql .= " c ";
break;

endswitch;
#7

[eluser]Wayne Smallman[/eluser]
[quote author="progr@mmer" date="1295139468"]you can still use the switch, you just need to set a variable at the top

Code:
$aos = isset($array_options['options']['show']) ? $array_options['options']['show'] : 'all';
switch($aos):

case 'mine':
$sql .= " a ";            
break;

case 'friends':
$sql .= " b ";
break;

case 'all':
default:
$sql .= " c ";
break;

endswitch;
[/quote]
Hi!

I'm already using a ternary statement elsewhere to determine the actual value of the variable, based on a segment, so the variable could never be empty:

Code:
$uri_segment['show'] = (empty($array_method_data['show'])) ? 'all' : $array_method_data['show'];

I'd be loathed to double up on code, as a work-around for some quirk perhaps in CodeIgniter or even PHP itself.

Thanks all the same.




Theme © iAndrew 2016 - Forum software by © MyBB