2013-01-15 26 views
-1

我從cakephp應用程序中的表sessionapps中獲取值。從cakephp中的數據庫獲取值時出錯

我index.ctp文件如下

<table> 
<tr><td>ID</td><td>Title</td><td>Post</td><td>Actions</td><td>Created On</td></tr> 
<?php foreach ($sessionapps as $post): ?> 
<tr><td><?php echo $post[Sessionapp][id];?></td> 
    <td><?php echo $data->Html->link($post[Sessionapp][title],array('controller'=>'sessionapp','action'=>'view',$post[Sessionapp][id]));?></td> 
    <td><?php echo $post[Sessionapp][post];?></td> 
    <td><?php echo $this->Html->link('Edit Post ', array('action' => 'editarticle', $post[Sessionapp][id]));?></td> 
    <td><?php echo $post[Sessionapp][created];?></td></tr> 
<?php endforeach; ?> 
</table> 

,我已經創造了控制器sessionapps_controller.php,並在其中加入行動指標如下

function index() 
    { 
       $this->set('sessionapps', $this->Sessionapp->find('all')); 
    } 

但它無法正常工作,並給予錯誤

Notice (8): Use of undefined constant Sessionapp - assumed 'Sessionapp' [APP/views/sessionapps/index.ctp, line 14] 

Notice (8): Use of undefined constant id - assumed 'id' [APP/views/sessionapps/index.ctp, line 14] 

我的數組$ sessionapps如下

Array ([0] => Array ([Sessionapp] => Array ([id] => 1 [title] => The title [body] => This is the post body. [postby] => [created] => 2013-01-15 11:35:13 [modified] =>)) [1] => Array ([Sessionapp] => Array ([id] => 2 [title] => A title once again [body] => And the post body follows. [postby] => [created] => 2013-01-15 11:35:13 [modified] =>)) [2] => Array ([Sessionapp] => Array ([id] => 3 [title] => Title strikes back [body] => This is really exciting! Not. [postby] => [created] => 2013-01-15 11:35:13 [modified] =>))) 1 
+0

也發佈你的錯誤。 – cartina

+0

看到編輯的問題.. –

+0

你有沒有嘗試我在你的視圖文件下面張貼的答案。您沒有正確引用數組元素。只需將foreach循環替換爲我發佈的那個。 – cartina

回答

1

我認爲它應該是這樣的。

<?php foreach ($sessionapps as $post): ?> 
<tr><td><?php echo $post['Sessionapp']['id'];?></td> 
    <td><?php echo $data->Html->link($post['Sessionapp']['title'],array('controller'=>'sessionapp','action'=>'view',$post['Sessionapp']['id']));?></td> 
    <td><?php echo $post['Sessionapp']['post'];?></td> 
    <td><?php echo $this->Html->link('Edit Post ', array('action' => 'editarticle', $post['Sessionapp']['id']));?></td> 
    <td><?php echo $post['Sessionapp']['created'];?></td></tr> 
<?php endforeach; ?> 
+0

不工作...我已經用它了.. –

+0

@Usman ok。然後嘗試print_r($ sessionapps);它給了什麼結果? – cartina

+0

@Usman查詢$ this-> Sessionapp-> find('all')沒有給出結果。所以$ sessionapps不包含你想在視圖文件中顯示的元素 – cartina