2013-02-28 21 views
4

我在我的視圖中有這個錯誤,無法找出問題所在。Foreach在codeigniter視圖上的undefined變量錯誤

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: c 

Filename: views/commentsList.php 

Line Number: 10 

這是我的視圖代碼

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head> 
<body> 

<div id="commentsByParentId"> 
    <?foreach($comments as $c):?> 
     <?=$c['comment']?> 
    <?endforeach?> 

</div> 
</body> 
</html> 

$comments是陣列來自控制器到來。 $c是循環變量,所以我不明白爲什麼它捕捉到undef var error

UPD:這裏是我的控制器代碼

public function viewCommentsListByParentId($parentid) { 
    $data = array(); 
    $data = $this->em->getRepository('Entities\Comment')->findBy(array('parentid' => $parentid)); 
    $comments = array(); 
    for ($i=0; $i<count($data); $i++){ 
     $comments[$i]['comment'] = $data[$i]->getComment(); 
    } 
    $this->load->view('commentsList', $comments); 
} 
+0

發表您的陣列結構 – 2013-02-28 08:21:20

+0

你會想看看$意見陣列實際上有任何數據。嘗試在forloop之前執行'print_r($ comments);'。 – Jeemusu 2013-02-28 08:25:15

+0

顯示控制器代碼,解決問題要容易得多 – Ziinloader 2013-02-28 08:28:35

回答

1

你們的服務器允許<?標籤。有些配置不會,他們會將它們轉換爲常規的html註釋。試試<?php foreach($comments as $c): ?>,看看是否能解決問題。

編輯:

現在,我們修正了錯誤,它並不知道$意見是什麼。這是因爲在你的控制器,你將它定義爲$ commentsList:

所以你需要<?php foreach($commentsList as $c): ?>

注意:您仍然無法使用<?標籤,因爲這些是擺在首位的問題。

第二個編輯:

我擡頭CodeIgnitor的文檔,它說你必須在$this->load->view('view_name', $data);包括其中的數據是值的數組,其中的關鍵是在視圖中使用的變量名和值該密鑰的值。

那麼試試這個:$this->load->view('view-name', array('comments' => $comments));

然後在視圖回到<?php foreach($comments as $c): ?>

this的細節

+0

對'<?='行也不會做同樣的事情那麼,不是產生任何錯誤? – sevenseacat 2013-02-28 08:25:30

+0

可能值得將它們全部切換到正常版本,只是爲了確保。 – Jeemusu 2013-02-28 08:27:17

+1

通常<?=允許但<?不是。我嘗試在我的機器上運行代碼,並且我很困惑$ c沒有被定義。然後我發現它已經註釋掉了<?標籤,但不包含<?=標籤。有些機器會這樣做,這取決於你的php配置。 – yourdeveloperfriend 2013-02-28 08:28:03

0

看你的控制器代碼後: 您需要在關聯數組傳遞$意見看法:

$data['comments'] = $comments; 
$this->load->view('commentsList', $data); 
+0

我試過了。結果是一樣的。未定義變量c – 2013-02-28 08:22:10

+0

確定然後,執行'print_r($ comments); exit;'在foreach之前檢查輸出 – codefreak 2013-02-28 08:23:01

+0

Array([0] => Array([comment] => new comment 1)[1] => Array([comment] => new comment 1)[2] => Array([comment] => new comment 1)[3] => Array([comment] => new comment 1)[4] => Array [comment] =>新評論1)[5] => Array([comment] => wqwq)[6] => Array([comment] => wqwq)[7] => Array([comment] => fd )[] => Array([comment] => fd)[9] => Array([comment] => fdsadfdsf)[10] => Array([comment] => fdsadfdsf)[11] => Array [評論] =>一個更多評論)[12] =>數組([評論] => dfgsgdsfg)[13] =>數組([評論] =>一個更多評論)) – 2013-02-28 08:26:24

0

試試這個:

<?php 
foreach($comments as $c){ 
    echo $c['comment']; 
} 
?> 
0

奇怪的錯誤,你到了那裏。爲什麼在foreach循環中沒有HTML時使用速記PHP標籤?或者生產代碼不同?

您可以通過使用下面的代碼(變通)使你的代碼工作:

<?php 
    foreach($comments as $key=>$c) { 
     if(isset($c) && isset($c['comment'])) { 
      echo $c['comment']; 
     } else { 
      echo 'Error at index ' . $key; 
     } 
    } 
?> 

請報告你得到返回的錯誤如何馬努。

+0

一個PHP錯誤遇到 嚴重性:注意 消息:未定義的變量:評論 文件名:觀點/ commentsList.php 行號:11 一個PHP錯誤遇到 嚴重性:警告 消息:爲foreach提供的參數無效() 文件名:views/commentsList.php 行號:11 – 2013-02-28 09:34:21

+0

Uhhh foreach有一個無效的參數嗎?很奇怪..'$ comments'是一個數組嗎?因爲'$ key => $ c'是正確的PHP:http://php.net/manual/en/control-structures.foreach.php – 2013-02-28 10:21:59

+0

我回答了我自己的問題,解決了問題 – 2013-02-28 10:32:08

0

大家好,謝謝。我用這種方法解決了這個問題。我改變了我的控制器。傳遞控制器對象數組比數組數組似乎更好。

public function viewCommentsListByParentId($parentid) { 
    $data = array(); 
    $data['comments'] = $this->em->getRepository('Entities\Comment')->findBy(array('parentid' => $parentid)); 
    $this->load->view('commentsList', $data); 
} 

,並改變了我的看法這

<?php foreach($comments as $c):?> 
     <?=$c->getComment()?><br> 
    <?php endforeach?>