Welcome Guest, Not a member yet? Register   Sign In
Strange Message: Undefined variable error
#1

[eluser]jstuardo[/eluser]
Hello !!

I have just started using CodeIgniter and I found the first strange problem.

Please, look at this code I have in a view:

Code:
foreach ($usuarios as $usuario)
        $class_suffix = $ndx % 2 == 0 ? 'Inverse' : '';
        echo "  <tr>
                  <td class='tableRow'>$usuario->id</td>
                  <td class='tableRow'>$usuario->username<td>
                  <td class='tableRow'>$usuario->nombres</td>
                  <td class='tableRow'>$usuario->apellidos</td>
                  <td class='tableRow'>$usuario->email</td>
                  <td class='tableRow'>$usuario->fecha_registro</td>
                  <td class='tableRow'>$usuario->fecha_visita</td>
                  <td class='tableRow'>$usuario->activo</td>
                </tr>";

where $usuarios is a view variable assigned with "$data['usuarios'] = $this->Usuario_model->get_usuarios($start, $reg_x_pag);" in controller.

Initially, $usuarios is empty.

When I run the page, I get several errors concerning $usuario declaration:

------
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: usuario

Filename: views/lista_usuarios.php

Line Number: 50
------
A PHP Error was encountered
Severity: Notice

Message: Trying to get property of non-object

Filename: views/lista_usuarios.php

Line Number: 50
------

Where line number 50 is:

Code:
<td class='tableRow'>$usuario->id</td>

Well. The strange thing is that if I comment the line "$class_suffix = $ndx % 2 == 0 ? 'Inverse' : '';", the errors are gone.

Any help will be greatly appreciated. PHP automatically declare variables so I cannot realize why this error is happening and dependant on a line that doesn't even use the mentioned variable.

Thanks
Jaime
#2

[eluser]TheFuzzy0ne[/eluser]
Please post a print_r() of $usuarios. I suspect that it, like the error suggests, doesn't contain any objects.
#3

[eluser]jedd[/eluser]
Hi Jaime,

[quote author="jstuardo" date="1244841758"]
PHP automatically declare variables so I cannot realize why this error is happening ...[/quote]

Ahh, this isn't strictly true - there are certain circumstances that I bump up against in CI a lot more than 'raw' PHP (and it's been so long since I've used that, that my memory about it may be flaky) where you do get errors if you try to use a variable before setting it, even to an empty string say.
#4

[eluser]jstuardo[/eluser]
Hello... in fact $usuario is not an object yet, because $usuarios array is empty, so before foreach loop I used if (count($usuarios)) and the problem was solved.

Thanks
Jaime

[quote author="TheFuzzy0ne" date="1244842708"]Please post a print_r() of $usuarios. I suspect that it, like the error suggests, doesn't contain any objects.[/quote]
#5

[eluser]TheFuzzy0ne[/eluser]
If you set the variable to an empty array() as soon as you can, you shouldn't need any checks in there at all. If the array is empty, the foreach loop won't execute.
Code:
$usuarios = array();

foreach ($usuarios as $usuario)
{
    $class_suffix = $ndx % 2 == 0 ? 'Inverse' : '';
    echo "  <tr>
        <td class='tableRow'>$usuario->id</td>
        <td class='tableRow'>$usuario->username<td>
        <td class='tableRow'>$usuario->nombres</td>
        <td class='tableRow'>$usuario->apellidos</td>
        <td class='tableRow'>$usuario->email</td>
        <td class='tableRow'>$usuario->fecha_registro</td>
        <td class='tableRow'>$usuario->fecha_visita</td>
        <td class='tableRow'>$usuario->activo</td>
        </tr>";
}

Whilst editing the above example, I just realised that you are missing the curly braces from your foreach call, that's what the problem is (I've added them above).




Theme © iAndrew 2016 - Forum software by © MyBB