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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2577968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:59:00  来源:igfitidea点击:

PHP - CodeIgniter - Invalid argument supplied for foreach()

phphtmlcodeignitererror-handlingwarnings

提问by Dzung Nguyen

I try to write a site with CodeIgniter but I've a problem with PHP. I'm sure that it's so simple and can't be wrong. But I don't know bugs from , just a newbie of CodeIgniter :)

我尝试使用 CodeIgniter 编写一个站点,但是我遇到了 PHP 问题。我相信这很简单,不会出错。但我不知道错误,只是 CodeIgniter 的新手:)

    <html>  
    <head>  
        <title><?=$page_title?></title>  
    </head>  
    <body>  
        <?php foreach($result as $row):?>  
        <h3><? echo $row->title; ?></h3>  
        <p><? echo $row->text; ?></p>  
        <?php endforeach;?>  
    </body>  
</html> 

I've a bug from this file :

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

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/helloworld_view.php

Line Number: 6

遇到 PHP 错误

严重性:警告

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

文件名:views/helloworld_view.php

行号:6

thanks in advance for reading this :)

提前感谢您阅读本文:)

采纳答案by Robert Hurst

The variable you supply to the foreach loop has to be an array. You can skip the foreach if the value of the variable supplied is not an array with the solution below.

您提供给 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; ?>

回答by Alex Lawford

Try foreach($result->result() as $row)- it could be you're trying to iterate through the object returned by Codeigniter's active record.

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

回答by Phil Sturgeon

If you are wondering what could be in the variable, output it!

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

var_dump($result);

That will instantly tell you what is going on. My guess, you have returned FALSE somewhere from your model, or you are using the DB object and not result() or result_array() (as suggested by Alex).

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

回答by confiq

$result is not array.

$result 不是数组。

Try to check it with is_arraybefore foreach.

尝试使用is_arraybefore检查它foreach

And debug why $result is not array :P

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

回答by Zeral

You can use the emptyphp function and do something like

您可以使用空的php 函数并执行类似的操作

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

回答by nick

First, you need to make sure that the data array that you are passing to your view is indeed called $data['result'].

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

In the controller page, it should look something like:

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

// 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');
}

In the view page it should be

在视图页面中它应该是

<?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>";  
}
?>

回答by Chintan Thummar

You need to define $data['result'] in controller

您需要在控制器中定义 $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;
} 

回答by Hafiz Shehbaz Ali

Dude, This error "Invalid argument supplied for foreach()" mostly occur. When you are passing the associative array to foreach is null. Carefully check the your associative array using echo statement. Don't pass the null associative array to foreach loop.

伙计,这个错误“为 foreach() 提供的参数无效”主要发生。当您将关联数组传递给 foreach 时为空。使用 echo 语句仔细检查关联数组。不要将空关联数组传递给 foreach 循环。

回答by Hook

Data of your is db->result_arrayor that resutl()object in perhaps $resultis array Your $result['text'];in View if not. $resultis array object you must try print_ror var_dump $resultin controller

如果不是,您的数据db->result_array或该resutl()对象可能$result是数组您$result['text'];在视图中。$result是您必须尝试print_rvar_dump $result在控制器中的数组对象



回答by rony apri benta sitepu

This may help

这可能有帮助

function query($query){
    global $conn;
    $result = mysqli_query($conn, $query);
    $rows = [];
    while($row = mysqli_fetch_assoc($result) ) {
        $rows[] = $row;
    }
    return $rows;<<<< check again
}