0

遇到 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');
    }   
}
?>

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

4

2 回答 2

4

你没有从你的函数中返回任何东西。

你可能想要这样的东西:

public function get_mat()
  {
    $query = $this->db->get('materials');
    return $query->result_array(); 
  }
于 2012-11-09T16:17:59.320 回答
0

杰伦-是对的。在模型中不返回任何值,因此实际上发生了这样的错误。我给你建议请检查从数据库中检索的数据,如..

      $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; 

因此,不会出现此类错误。

于 2012-11-09T16:27:10.647 回答