2016-04-24 47 views
0

當我使用的foreach顯示在底部的我的價值,它給我一個假值 控制器:CakePHP的的foreach

public function index_hote() { 
    $this->Reservation->recursive = 1; 
    $this->loadModel("Bien"); 
    $biens=$this->Bien->find('first',array('conditions'=>array("Bien.idBien  =idBien"))); 
    $reservations=$this->Reservation- >find('first', 
    array('conditions'=>array('Reservation.idBien'=> $biens['Bien'] ['idBien']))); 
     Debugger::dump($reservations['Reservation']); 
    $this->paginate('Reservation'); 
    $this->set('reservations', $reservations['Reservation']); 

    } 

在視圖:

<?php foreach ($reservations as $reservation): ?> 
    <tr> 
    <?php echo h($reservation) ?> 
    <td><?php echo h($reservation['dateReserDu']); ?>&nbsp;</td> 
    <td><?php echo h($reservation['dateReserAu']); ?>&nbsp;</td> 
    <td><?php echo h($reservation['montantAPaye']); ?>&nbsp;</td> 
    <td><?php echo h($reservation['etatReservation']); ?>&nbsp;</td> 
     </tr> 
    <?php endforeach; ?> 

當我調試它顯示我正確的價值(做Debugger::dump($reservations['Reservation']);後的PIC):

the pic after doing Debugger::dump($reservations['Reservation']);

但在表它顯示我一個假值:

pic of the false value

當我做<?php echo h($reservation) ?>它顯示我正確的價值,但是當我添加屬性如h($reservation['dateReserDu']); ?>這是行不通的PLZ爲什麼?我什麼時候可以做?

+0

請告訴我們你剛纔'調試($預約);'歡呼 –

+0

陣列(' Reservation'=> array('idReser'=>'1', 'dateReserDu'=>'2016-04-23','dateReserAu'=>'2016-04-27', 'montantAPaye'=>'500 ','etatReservation'=>'attente', 'iddev'=>'0','idBien'=>'9', 'id'=>'24'), \t'Personne'=> array('login'=>'*****','id'=>'24','designation'=>'client','nom'=>'mohamed',' prenom」 => 'jmaa', 'dateNaiss'=> '1991年7月22日', \t \t '住址'=> '芸香巴黎', \t \t 'telFixe'=> '26119945', \t \t 'telMobile1'=> '95632114', \t \t 'telMobile2'=> '98745213', \t \t '電子郵件'=> '[email protected]', \t \t 'MDP'=> '26111991', \t \t 'lienPhotoPersonnel'=> 'sou.jpg', \t \t 'idVille'=> '14' \t) \t ) – wah

回答

1

爲什麼你在這裏使用循環?您已使用find('first')檢索一個數據,請參閱 doc

如果要檢索所有數據,請使用find('all')。但是,如果你想echo您發現第1個數據只是使用下面的結構

$this->set('reservations', $reservations); 

鑑於

echo $reservations['Reservation']['Your_field_name'];

+0

如果我回聲$保留[ '預約'] [ 'Your_field_name']; 我有同樣的問題:( – wah

+0

您是否更改$ this-> set,我提到了?並且您刪除了foreach循環嗎? –

+1

它工作; D感謝您:* – wah