遇到PHP错误严重性:警告消息:为foreach()提供的参数无效

时间:2017-05-13 05:29:41

标签: php codeigniter

im new code in codeigniter

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()  
Filename: views/siswa_spp.php
Line Number: 39

我无法解决这个错误,这是我的控制器:

function siswa($id_siswa)
  {
      $data['spp']            = $this->M_spp->detail_spp($id_siswa)->row();
      $data['temp']           = $this->M_spp->temp_spp($id_siswa)->row();
      $data['list']           = $this->M_spp->list_temp($id_siswa)->result();
      $data['bulan']          = $this->M_spp->listbulan($id_siswa);

      $this->load->view('includes/head');
      $this->load->view('includes/nav_header');
      $this->load->view('includes/nav_sidebar');
      $this->load->view('siswa_spp',$data);
      $this->load->view('includes/foot');
  }

我的模特:

function listbulan($id_siswa)
  {
    $this->db->select('*');
    $this->db->from('tb_spp');
    $this->db->where('id_siswa',$id_siswa);

    $query = $this->db->get();

    if ($query->num_rows() > 0) {
      $result = $query->result();
    }

    return $result;
  }

和我的观点:

<?php
   foreach ($bulan as $b) {
    echo $b->bulanPembayaran;
   }
?>

我不知道我的代码中的错误在哪里,对不起英文不好

2 个答案:

答案 0 :(得分:2)

检查模型是否找到对象

function listbulan($id_siswa)
  {
    $this->db->select('*');
    $this->db->from('tb_spp');
    $this->db->where('id_siswa',$id_siswa);

    $query = $this->db->get();

    $result = false;
    if ($query->num_rows() > 0) {
      $result = $query->result();
    }

    return $result;
  }

检查你的观点。

<?php
   if($bulan){
      foreach ($bulan as $b) {
        echo $b->bulanPembayaran;
      }
   }
?>

答案 1 :(得分:0)

首先检查$ data [&#39; bulan&#39;]是否为数组

$ this-&gt; load-&gt; view(&#39; siswa_spp&#39;,数组(&#34;数据&#34; =&gt; $ data [&#39; bulan&#39;])) ;

在视图中传递这样...... !!