dousilie9522 2016-10-04 08:22
浏览 19

Cakephp - 使用'foreach'显示数据不会返回任何值

I am trying to digest one of cakephp tutorials and yesterday I stuck. The sample website is called Jobfinds, so I have table name "jobs".

Here is the code from JobsController :

    <?php
    namespace App\Controller;

    use App\Controller\AppController;

    class JobsController extends AppController{


    public $name = 'Jobs';

    /*
     * Default Index Method
     */

public function index() {
    //Set Query Options
    $options = array(
        'order' => array('Jobs.created' => 'asc' ),
        'limit' => 4
    ); 

    //Get Job Info

    $jobs = $this->Jobs->find('all', $options);

    $this->set('jobs', $jobs);
  }
}
?>

This code is in index.ctp but it doesn't show anything but it should.

<?php foreach($jobs as $job) : ?>
<p><?php  echo $job['jobs']['title'];
 ?> </p>
<?php endforeach; ?>

When I put for example

<?php echo "something"; ?>

Then 'something' is being displayed so I figured that there must be just a small mistake that is does not fetch data from the table. I was trying for too long. I'm starting loosing my hair because of that. This tutorial is pretty awesome but there is used cakephp 2.x and I am using 3.x. so on every step I need to change something and up till now I succeeded but I stuck with this for good. Anyone can see where is the culprit? Why it doesn't fetch data from the table?

I have no errors in debug LOGS.

print_r($job) inside loop gives as below

    Cake\ORM\Entity Object ( [id] => 1 [category_id] => 7 [user_id] => 1          [type_id] => 1 [company_name] => Tech Guy Computer Services [title] => Senior Graphic Designer [description] =>

Lorem ipsum dolor sit amet(more description in lorem ipsum

[city] => Burlington [state] => MA [contact_email] => contact@employer.com [created] => Cake\I18n\FrozenTime Object ( [time] => 2014-07-14T00:00:00+00:00 [timezone] => UTC [fixedNowTime] => ) [[new]] => [[accessible]] => Array ( [*] => 1 ) [[dirty]] => Array ( ) [[original]] => Array ( ) [[virtual]] => Array ( ) [[errors]] => Array ( ) [[invalid]] => Array ( ) [[repository]] => Jobs )
Cake\ORM\Entity Object ( [id] => 2 [category_id] => 7 [user_id] => 1 [type_id] => 1 [company_name] => Tech Guy Computer Services [title] => UX Designer [description] =>
  • 写回答

3条回答 默认 最新

  • dougai3418 2016-10-04 08:53
    关注

    Try this code it's will help you.

    <?php foreach($jobs as $job) : ?>
     <p><?php  echo $job['title'];?></p>
    <?php endforeach; ?>
    

    Thank you!

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀