0

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.

10
  • 2
    are short tags enabled? If not, do <?php echo rather than <?= same for <? do <?php Commented Sep 22, 2015 at 20:27
  • 1
    use <?php foreach ?> <?php endforeach;?> Commented Sep 22, 2015 at 20:47
  • 1
    someone posted an answer. I should probably be doing that. @TomásDornasPerone to set the record straight ;-) Commented Sep 22, 2015 at 20:48
  • 1
    @Fred-ii-: you better!!! otherwise this doesn't make sense Commented Sep 22, 2015 at 20:49
  • 1
    @Vickel Cheers and I quoted your comment. Commented Sep 22, 2015 at 20:53

2 Answers 2

5

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"

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.