PHP - CodeIgniter - Invalid argument supplied for foreach()
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 :)
Try foreach($result->result() as $row)
- it could be you're trying to iterate through the object returned by Codeigniter's active record.
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; ?>
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).
$result is not array.
Try to check it with is_array
before foreach
.
And debug why $result is not array :P
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/>";
}
?>
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)
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>";
}
?>
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;
}
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.
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
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
}
精彩评论