Notice (8): Undefined variable: animal [CakePHP2]

Par Maxou, il y a 10 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je faisais

Dans View\Animaux\index.ctp j'avais :

<header class="page-header"> <h1><?php echo $all['Categorie']['nom'] ?></h1> </header> <?php echo $this->Html->link( 'Ajouter un Animal', array('controller' => 'animaux', 'action' => 'add') ); ?> <section id="animal" class="row"> <?php foreach ($animaux as $animal): ?> <div class="row"> <div class="col-md-2"> <?php echo $animal['Animal']['titre']; ?> </div> <div class="col-md-3"> <?php echo $animal['Animal']['com']; ?> </div> <div class="col-md-3"> <?php echo $this->Html->image($animal['Animal']['img'], array('class' => 'img-responsive', 'height' => 300, 'width' => 200 )); ?> </div> <div class="col-md-2"> <?php echo $this->Html->link( 'Editer', array('action' => 'edit', $animal['Animal']['id'])); ?> </div> <div class="col-md-2"> <?php echo $this->Form->postLink( 'Supprimer', array('action' => 'delete', $animal['Animal']['id']), array('confirm' => 'Etes-vous sûr ?')); ?> </div> </div> <?php endforeach; ?> <?php unset($animal); ?> </section>

tout ceci marchait très bien

Ce que je fais maintenant

Maintenant dans View\Animaux\index.ctp j'ai :

<header class="page-header"> <h1><?php echo $all['Categorie']['nom'] ?></h1> </header> <?php echo $this->Html->link( 'Ajouter un Animal', array('controller' => 'animaux', 'action' => 'add') ); ?> <section id="animal" class="row"> <?php foreach ($animaux as $animal): ?> <?php echo $this->element('animal'); ?> <?php endforeach; ?> <?php unset($animal); ?> </section>

et puis j'ai réalisé dans View\Elements\animal.ctp

<div class="row"> <div class="col-md-2"> <?php echo $animal['Animal']['titre']; ?> </div> <div class="col-md-3"> <?php echo $animal['Animal']['com']; ?> </div> <div class="col-md-3"> <?php echo $this->Html->image($animal['Animal']['img'], array('class' => 'img-responsive', 'height' => 300, 'width' => 200 )); ?> </div> <div class="col-md-2"> <?php echo $this->Html->link( 'Editer', array('action' => 'edit', $animal['Animal']['id'])); ?> </div> <div class="col-md-2"> <?php echo $this->Form->postLink( 'Supprimer', array('action' => 'delete', $animal['Animal']['id']), array('confirm' => 'Etes-vous sûr ?')); ?> </div> </div>

Ce que j'obtiens

Notice (8): Undefined variable: animal, je n'arrive pas à définir ma variable animal dans mon fichier View\Elements\animal.ctp

Pouvez-vous me guider pour une démarche ? merci d'avance

5 réponses

Flitflit, il y a 10 ans

dans index.ctp essaye plutot :

echo $this->element('animal', array( 'animal' => $animal ));

car tu dois faire passer les variables dans l'element. Voir la doc ici.

Maxou, il y a 10 ans

c'est exact, merci de ton retour j'étais bien sur la bonne doc mais impossible de comprendre correctement.

Maxou, il y a 10 ans

Je suis un peu bête car je l'ai déjà fait avec mon menu x)

<?php echo $this->element('menu', array('menu' => $menu)); ?>
Flitflit, il y a 10 ans

Faut avouer que c'est pas très logique aussi. dans Cake3 les variables sont dispo sans avoir à les passer comme ça en paramètre pour info ;)

Maxou, il y a 10 ans

ahah alors ça va ma logique n'est pas perdue à tout jamais :3