2013-10-02 25 views
0

我試圖從控制器傳遞數組數據到視圖,但視圖丟失了「Yii格式」(標題引導程序),標題不顯示,只顯示「平面文字」。Yii和array_pop在視圖中到達作爲第二個參數

當我嘗試使用array_pop提取元素時發生這種情況。

查看:magazines.php

<?php 
/* @var $this SiteController */ 

$this->pageTitle=Yii::app()->name; 
?> 
<h3> 
    <?php  
     $aux_string = ""; 
     foreach ($files_format as $aux_string) { 
      $aux_string->array_pop($files_format); 
     } 
    ?> 
</h3> 

控制器:SiteController.php

public function actionMagazines() 
{ 
      $path="pdfs/"; 
      $directory=dir($path); 
      $files=array(); 
      while (false !== ($entry = $directory->read())) 
      { 
       if($entry!="."&&$entry!=".."){ 
        array_push($files, $entry); 
        print_r($files); 
       } 
      } 

      $this->render('magazines', array('files_format'=> $files)); 
} 

以另一種方式,如果我不使用array_pop的 「Yii的格式」 保持不變(下面,代碼行與array_pop是評論與視圖正確顯示)

  ... 
      //$aux_string->array_pop($files_format); 
      ... 

回答

0

我想你想要做的是:

$aux_string = ""; 
    foreach ($files_format as $string) { 
     $aux_string.=$string ; 
    } 

或:

$aux_string = join('',$files_format); 
+0

不完全;不過,你已經完成了,我明白了我用foreach構造犯的錯誤。乾杯克勞德! –

+0

錯誤是我不瞭解foreach子句。我認爲還需要提取循環中的值(放入方括號中)。另一方面,這是foreach(迭代器)。 –

0

真的,錯誤是由foreach循環改變一個代碼行解決。爲此,我明白「查看」的問題是由於手動處理堆棧;而foreach正在內部進行自動模式。

查看:magazines.php

<?php 
/* @var $this SiteController */ 

$this->pageTitle=Yii::app()->name; 
?> 
<h3> 
    <?php  
     $aux_string = ""; 
     foreach ($files_format as $aux_string) { 
      echo $aux_string."<br><br>"; 
     } 
    ?> 
</h3> 
相關問題