Undefined variable CodeIgniter?

i'm having an issue with my php. I've been using codeigniter and the following code is returning me this error message:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: item
Filename: views/banner.php
Line Number: 37

My controller is as following:

public function __construct()
{
    parent::__construct();

    $this->auth->check();

    $this->load->database();
    $this->load->helper('url');
    $this->load->model('banner_model');
}

public function index()
{
    $dados['banner'] = $this->banner_model->recuperar_todos_banners();

    $this->load_view("banner", $dados);
}

My view:

    <? foreach ($banner as $item):?>
       <tr>
          <td><?=$item->chave?></td>
          <td><?=$item->imagem?></td>
          <td><?=$item->descricao?></td>
          <td><?=$item->link?></td>
          <td>
             <button type="button" class="btn btn-danger btn-xs">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
             </button>
          </td>
       </tr>
    <? endforeach; ?>

I tested both variables using var_dump. The purpose of this code is to access a MySQL database and store the result in an array. The $banner variable is working fine and all the data is there so each position is occupied by objects, each object being a table row.

I know this question has been asked before on StackOverflow but I still couldn't figure out what's going on exactly.

Upvotes: 0

Views: 473

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74230

Me:

are short tags enabled? If not, do <?php echo rather than <?= same for <? do <?php – Fred -ii- 23 mins ago"

OP:

Sorry. you were correct. I fixed only some of the tags and forgot others. – Tomás Dornas Perone"

The issue was indeed short tags after all.

And a comment by Vickel, and I quote:

use <?php foreach ?> <?php endforeach;?> – Vickel 4 mins ago"

Upvotes: 5

Konstantin Rachev
Konstantin Rachev

Reputation: 310

See if $banner is multidimensional array, or debug using a test foreach in your controller, hit a var_dump() in both places of the $item.

Upvotes: 2

Related Questions