【问题标题】:Invalid argument supplied for foreach() where is mistake?为 foreach() 提供的参数无效哪里出错了?
【发布时间】:2012-10-29 23:47:30
【问题描述】:

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

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

【问题讨论】:

标签: php mysql codeigniter arguments syntax-error


【解决方案1】:

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

你可能想要这样的东西:

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

【讨论】:

    【解决方案2】:

    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; 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-26
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 2011-02-07
      相关资源
      最近更新 更多