foreach不在cakephp中打印查询结果

时间:2013-06-18 17:10:07

标签: mysql cakephp foreach cakephp-2.0 cakephp-2.1

我的控制器

        $data= $this->Contact->find('all',array(
        'conditions' => array('Contact.User_id' => $id)));

    $this->loadModel('Calllog');
      foreach($data as $row){

      $mobile=  $row['Contact']['mobileNo'];
      $home=  $row['Contact']['homeNo'];
      $work=  $row['Contact']['workNo'];


      $recent=$this->Calllog->find('all',
          array('order'=>'Calllog.idCallLog DESC',
              'limit' => 1,
              'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
                  'Calllog.User_id' => $id


              )));

    debug($recent);
      $this->set('recent',$recent);


    }

在我看来我有这条线

  <?php  foreach($recent as $as): ?> <?php echo $as['Calllog']['mobileNo'];?>

但是没有显示任何错误

我调试结果,我得到一行意味着工作正常 我也转储sql查询,其中查询运行正常,当我在phpmyadmin运行此查询时,它给了我一行,这是我想要的..但​​为什么它不回应我的视图页面上的结果

调试结果

      array(
      (int) 0 => array(
       'CallLog' => array(
       'idCallLog =>'3',
        'mobileNo'=>'12345'
         'User_id'=>'1'
          )))

然后经过4行

         array()

1 个答案:

答案 0 :(得分:0)

试试这个

$data= $this->Contact->find('all',array(
        'conditions' => array('Contact.User_id' => $id)));

$recent = array();

$this->loadModel('Calllog');
foreach($data as $row){

  $mobile=  $row['Contact']['mobileNo'];
  $home=  $row['Contact']['homeNo'];
  $work=  $row['Contact']['workNo'];


  $recent[]=$this->Calllog->find('all',
          array('order'=>'Calllog.idCallLog DESC',
              'limit' => 1,
              'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
                  'Calllog.User_id' => $id
          )));

}

$this->set('recent',$recent);
debug($recent);