为foreach()提供的参数无效,哪里出错?

时间:2012-11-09 16:15:22

标签: php mysql codeigniter arguments syntax-error

  

遇到PHP错误

     

严重性:警告

     

消息:为foreach()提供的参数无效

     

文件名:views / content_view.php

     

行号:8

型号:

<?php
if ( ! defined('BASEPATH')) exit ('No direct script access allowed');
  class Mat_model extends CI_Model
  {
    public function get_mat()
    {
      $query = $this->db->get('materials');
      $query->result_array(); 
    }
  }
?>

查看:

<div id="breadcrumb"><a href="">Home</a> &raquo; <a href="">Somewhere</a></div>
    <div id="right">
      <h1>Lorem Ipsum Dolor Set Amir</h1>
      <span class="postinfo"> Posted by <a href="">Dylan</a> on 07.09.06</span>
      <p>Lorem ipsum dolor set amir tolos and tacos for plenty to see. jack is the orange sub marine, by any means I found my plans. </p>
      <hr />
      <h1>
      <?php foreach ($news as $one):?>
      <?if(is_empty($one))?>
      <?=$one['author']?>
      <?php endforeach; ?>
      </h1>
      <span class="postinfo"> Posted by <a href="">Dylan</a> on 07.09.06</span>
      <p>Lorem ipsum dolor set amir <a href="">tolos</a> and tacos for plenty to see. jack is the orange sub marine, by any means I found my plans. </p>
    </div>
  </div>

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
    public function index()
    {
        $this->load->view('header_view');
        $this->load->view('menu_view');
        $this->load->view('categories_view');
        $this->load->view('useful_sites_view');
        $this->load->model('mat_model');
        $data = array();
        $data['news'] = $this->mat_model->get_mat(); 
        $this->load->view('content_view',$data);
        $this->load->view('footer_view');
    }   
}
?>

那我的错误在哪里?我找不到了。

2 个答案:

答案 0 :(得分:4)

你没有从你的功能中返回任何东西。

您可能需要以下内容:

public function get_mat()
  {
    $query = $this->db->get('materials');
    return $query->result_array(); 
  }

答案 1 :(得分:0)

Jeroen-是对的。在模型中没有返回任何值,所以实际上发生了错误。我建议你检查从数据库中检索的数据,如..

      $this->load->view('useful_sites_view');
      $this->load->model('mat_model');
      $data = array();
      $data['news'] = $this->mat_model->get_mat(); 
      echo "<pre>";print_r($data);exit; 

所以,没有这种错误的可能性。