PHP - CodeIgniter - Invalid argument supplied for foreach()

对着背影说爱祢 提交于 2019-12-30 10:33:57

问题


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 :)

    <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

thanks in advance for reading this :)


回答1:


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.

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



回答2:


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




回答3:


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).




回答4:


$result is not array.

Try to check it with is_array before foreach.

And debug why $result is not array :P




回答5:


You can use the empty php function and do something like

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



回答6:


If you are using the tutorial at : http://net.tutsplus.com/tutorials/php/codeigniter-basics/

Then it is line 5 of the helloworld_model.php, it should be:

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

not

if ($query->num_rows() > 0)



回答7:


First, you need to make sure that the data array that you are passing to your view is indeed called $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>";  
}
?>



回答8:


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

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



回答9:


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.




回答10:


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





回答11:


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
}


来源:https://stackoverflow.com/questions/2577968/php-codeigniter-invalid-argument-supplied-for-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!