PHP-CodeIgniter-为foreach()提供的参数无效

PHP-CodeIgniter-为foreach()提供的参数无效,php,html,codeigniter,error-handling,warnings,Php,Html,Codeigniter,Error Handling,Warnings,我试图用CodeIgniter编写一个站点,但我在PHP方面遇到了一个问题。我相信这很简单,不会错的。但我不知道来自哪里的bug,只是CodeIgniter的一个新手:) 我有一个来自此文件的错误: 遇到一个PHP错误 严重性:警告 消息:为提供的参数无效 foreach() 文件名:views/helloworld_view.php 电话号码:6 提前感谢阅读:)$结果不是数组 在foreach之前,尝试使用is\u array检查它 并调试为什么$result不是数组:p提供给fore

我试图用CodeIgniter编写一个站点,但我在PHP方面遇到了一个问题。我相信这很简单,不会错的。但我不知道来自哪里的bug,只是CodeIgniter的一个新手:)


我有一个来自此文件的错误:

遇到一个PHP错误

严重性:警告

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

文件名:views/helloworld_view.php

电话号码:6

提前感谢阅读:)

$结果不是数组

foreach
之前,尝试使用
is\u array
检查它


并调试为什么$result不是数组:p

提供给foreach循环的变量必须是数组。如果提供的变量值不是具有以下解决方案的数组,则可以跳过foreach

<?php if(is_array($result)): ?>
<?php foreach($result as $row):?>  
<h3><? echo $row->title; ?></h3>  
<p><? echo $row->text; ?></p>  
<?php endforeach;?>  
<?php endif; ?>


尝试
foreach($result->result()作为$row)
-可能是您试图遍历Codeigniter的活动记录返回的对象。

如果您想知道变量中可能有什么,请输出它

var_dump($result);

这会立即告诉你发生了什么。我猜,您在模型的某个地方返回了FALSE,或者您使用的是DB对象,而不是result()或result_array()(如Alex所建议)。

如果您在以下位置使用教程:

然后是helloworld_model.php的第5行,应该是:

if ($query->num_rows() == 0)
不是

您可以使用php函数并执行以下操作

<?
    if(!empty($results)){
      echo "
      foreach($result as $row){
       <h3>".$row->title."</h3>  
       <p>".$row->text".</p>
           "; 
      }
    }else{
       echo "<p>no results<p/>";
    }
?>

首先,您需要确保传递给视图的数据数组确实被称为
$data['result']

在控制器页面中,它应该类似于:

// you need to put some data here for checking the number of results returned

if($numberOfRows > 0 ){
$data['result'] = $this->Yourmodel->methodName($arguments);

$this->load->view('yourView');
}

else{
$this->load->view('yourCustomMissingOrErrorView');
}
在查看页面中,它应该是

<?php

// note if you are just intitialiizing variables, remove the echo statements and put it     before all of your html. if you are looping for output then put it where it needs to go in the html

foreach($result as $value){
$title = $this->value->title; // just makes it easier to use if you need to use  elsewhere
$text = $this->value->text;    // just makes it easier to use if you need to use elsewhere  

echo "<h3>" . $title . "</h3>";
echo "<p>" . $text . "</p>";  
}
?>

您需要在控制器中定义$data['result']

//Controller File
function yourControllerMethod()
{
    $this->main();
    $this->load->model('yourModel');
    $data['result']  = $this->yourModel->getResultMethod();
    $this->load->view('yourView',$data);
}

//Model File
function getResultMethod()
{
    $this->db->from($this->yourTable);
    $query  = $this->db->get();
    $rows   = $query->result(); 
    return $rows;
} 

Dude,这个错误“为foreach()提供的参数无效”经常发生。将关联数组传递给foreach时,该值为null。使用echo语句仔细检查关联数组的属性。不要将空关联数组传递给foreach循环

您的数据是
db->result\u数组
或者
resultl()
对象中的
$result
可能是数组 您的
$result['text']在视图中
如果没有
$result
是数组对象,您必须在控制器中尝试
print\r
var\u dump$result

//Controller File
function yourControllerMethod()
{
    $this->main();
    $this->load->model('yourModel');
    $data['result']  = $this->yourModel->getResultMethod();
    $this->load->view('yourView',$data);
}

//Model File
function getResultMethod()
{
    $this->db->from($this->yourTable);
    $query  = $this->db->get();
    $rows   = $query->result(); 
    return $rows;
} 
这可能会有所帮助

function query($query){
    global $conn;
    $result = mysqli_query($conn, $query);
    $rows = [];
    while($row = mysqli_fetch_assoc($result) ) {
        $rows[] = $row;
    }
    return $rows;<<<< check again
}
函数查询($query){
全球$conn;
$result=mysqli\u查询($conn,$query);
$rows=[];
while($row=mysqli\u fetch\u assoc($result)){
$rows[]=$row;
}

返回$rows;这将停止错误,但不会帮助他了解到底发生了什么。Alex Lawford在这里做出了最好的猜测,要么他试图使用数据库对象,要么他从模型中的某个地方得到了FALSE。
function query($query){
    global $conn;
    $result = mysqli_query($conn, $query);
    $rows = [];
    while($row = mysqli_fetch_assoc($result) ) {
        $rows[] = $row;
    }
    return $rows;<<<< check again
}