Hey guys i need help. How to fix this?
Warning: Invalid argument supplied for foreach() in /####.com/dev/result.php on line 53
Here is the code on the line 53:Code:<?php foreach ($results as $result) { ?>
Hey guys i need help. How to fix this?
Warning: Invalid argument supplied for foreach() in /####.com/dev/result.php on line 53
Here is the code on the line 53:Code:<?php foreach ($results as $result) { ?>
I don't know much but after looking at some foreach examples, the only thing that comes to my mind is, are you missing this " } " ?
anyways have a look here:
http://www.tizag.com/phpT/foreach.php
My Name is Larry Hyman from Montreal. I fix computers on site, see my web site for more details:
http://hyman.sytes.net
Nope. I have the " } " ending tag.
When asking about code, always post a minimal test case, the smallest amount of code that illustrates the behavior you're writing about. In this case, you would need to show how $results is set and include the foreach loop's closing brace. This makes your question easier to understand, both for others and yourself. Sometimes the solution becomes obvious upon creating the minimal test case. Remember, we can only answer questions about the code you post. If it's incomplete, we'll waste time going over errors in the posted code that don't exist in your live code.
The error message means $results isn't traversable. If $results is the result of an SQL query, you can use a while loop:
or you can implement the Iterator interface and create an SQL result iterator.PHP Code:while ($result = $results->fetch()) {
...
}
Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.Misson, not Mission.
It means that $results is not like an array (or an object in php5).
Without showing the rest of the code there is no way to know what is going on.
Where did $results come from? If it came from a function call, some functions return NULL on an error do differentiate the outcome from an empty array.
Nothing is always absolutely so.
Descalzo is right, I got this error when what I supposed to be an array was an empty variable.
I really didn't expect PHP to work this way! :eek5:
The best way to use foreach is as below:
foreach ($array_variable as $key=>$value)
{
}
Description:
========
You have to use an array in place of $array_variable and $key represents here as the array index and the $value represents here as the $array_variable[$key].
I think you can understand. Please let me know if you have any more trouble to use foreach.
You should always check for the array to be initialized beforehand. Anyways, you can check the PHP doc for foreach.
█ Xavier L | Community Public Relations Manager (Free Hosting Support)
█ Yes, my position is too cool to even exist!
█ How am I helping? Rate this post by clicking theicon below! (this is even better than "liking" a post)
█ Terms of Service | Acceptable Use Policy | x10Hosting Wiki
If i give someone the FTP Access, can you fix it for me? i'll pay you with x10credits or something.