2017-08-31 75 views
1
$subjects = $this->Subjects 
    ->find('all', [ 
     'contain'=> [ 
      'Users' 
     ], 
     'fields'=> [ 
      'Users.username', 
      'Users.email' 
     ] 
    ]) 
    ->hydrate(false) 
    ->toArray(); 

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

我怎樣才能迴路主題控制器的索引視圖中的數據顯示這樣的形象如何在索引視圖

enter image description here

回答

1

示例結果:

<?php 
$array = array(
    0 => array(
     'math'=>40, 
     'english'=>40, 
     'history'=>40, 
     'science'=>40, 
     'user_id'=>64 
     'user'=> 
      array(
      'id' => 6 
      'name' => 'User', 
      'email' => '[email protected]' 
      ) 
    ) 
); 
?> 

這是根據您所提供的vardump示例代碼編寫:

<table> 
    <thead> 
    /// Give your table headers 
    </thead> 
<tbody> 
    <?php foreach($subjects as $subject) :?> 
     <tr> 
      <td><?=$subject['math']?></td> 
      <td><?=$subject['english']?></td> 
      <td><?=$subject['history']?></td> 
      <td><?=$subject['science']?></td> 
      <td><?=$subject['user_id']?></td> 
      <td><?=$subject['user']['id']?></td> 
      <td><?=$subject['user']['name']?></td> 
      <td><?=$subject['user']['email']?></td> 
     </tr> 
    <?php endforeach;?> 
</tbody> 

+0

哇謝謝你很多..我可以添加你的Skype – distromob

+0

如何打印id和user_id?它給了我錯誤 – distromob

+0

你能發佈你的查詢嗎?你檢索id和user_id? id和user_id來自哪個表? –

0

先做玩一些CSS打印相關的數據。希望這段代碼可以幫助你。

您需要對此進行一些更改。

'fields'=> [ 
     'Users.username', 
     'Users.email', 
     //add more fields that you want to display here 
] 



<table> 
    <thead> 
     <th>math</th> 
     <th>english</th> 
     <th>history</th> 
     <th>science</th> 
     <th>id</th> 
     <th>user_id</th> 
     <th>username</th> 
     <th>email</th> 
    </thead> 
    <tbody> 
     <?php foreach($subjects as $subject) :?> 
      <tr> 
       <td><?=$subject['Users']['match']?></td> 
       <td><?=$subject['Users']['english']?></td> 
       <td><?=$subject['Users']['history']?></td> 
       <td><?=$subject['Users']['science']?></td> 
       <td><?=$subject['Users']['id']?></td> 
       <td><?=$subject['Users']['user_id']?></td> 
       <td><?=$subject['Users']['username']?></td> 
       <td><?=$subject['Users']['email']?></td> 
      </tr> 
     <?php endforeach;?> 
    </tbody> 
</table> 
+0

感謝您的回答,但仍無法打印用戶名和郵箱地址 – distromob

+0

含義,你可以打印其他字段? –

+0

記住用戶名和電子郵件字段在用戶表中,是的,除了用戶名和電子郵件之外,所有的數據都會打印 – distromob

1

我測試了它。例如,這是您的主題數組。

<?php 
$array = array(
    0 => array(
     'Users'=> 
     array('name' => 'John Doe', 
     'email' => '[email protected]' 
     ) 
    ), 
    1 => array(
     'Users'=>array(
     'name' => 'Abs Doe', 
     'email' => '[email protected]' 
     ) 
    ), 
); 
?> 

這是循環的索引文件。

<table> 
    <thead> 
     <th>name</th> 
     <th>email</th> 
    </thead> 
    <tbody> 
     <?php foreach($array as $subject) :?> 
      <tr> 
       <td><?=$subject['Users']['name']?></td> 
       <td><?=$subject['Users']['email']?></td> 
      </tr> 
     <?php endforeach;?> 
    </tbody> 
</table> 

它按預期工作。

enter image description here

我不能給你答覆。因爲我無法訪問您的環境。但是,至少你可以知道哪些代碼需要修改。 希望得到這個幫助。從vardump

+0

我錯過了什麼?這是錯誤通知(8):未定義變量:數組[APP /模板\主題\ index.ctp,第27行] 警告(2):提供給foreach()的無效參數[APP/Template \ Subjects \ index.ctp ,第27行] – distromob

+0

這是測試數組。你需要用你的數組來替換。 –

+0

我與$替換araay受試者 ​​<= $主題[ '用戶'] [ '用戶名']> ​​<? = $ subject ['Users'] ['email']?> <?php endforeach;?>未定義的索引:用戶[APP/Template \ Subjects \ index.ctp,第29行,第30行,第31行,第32行和等 – distromob