mcmuney Posted July 18, 2011 Share Posted July 18, 2011 Just out of the blue, a fully functional page, without any changes made to it, is showing an error: Warning: Invalid argument supplied for foreach() in /path/file.php on line 169, line shown below: foreach($rs_frd as $offset => $value): How can I find out what's causing this error? Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 you will need to make sure that $rs_frd is an array Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244265 Share on other sites More sharing options...
Maq Posted July 18, 2011 Share Posted July 18, 2011 How is '$rs_frd' being generated? Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244266 Share on other sites More sharing options...
mcmuney Posted July 18, 2011 Author Share Posted July 18, 2011 Wow! That's some serious and messy code I just had to review. Looks like the error is appearing when there's no results for that array. Can I add something that will show a more friend error on the page if there's no results for that array or if array doesn't exist (in case of no data)? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244271 Share on other sites More sharing options...
teynon Posted July 18, 2011 Share Posted July 18, 2011 if ((isset($array))&&(is_array($array))) { foreach () { blah } } else { echo "Sorry. You are so ugly, it broke the script."; } Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244273 Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 I would recommend using empty instead of isset here, isset will return TRUE even if the variable is empty. Edit: going off of what Maq posted, somewhere in your code, where this array is generated is where you will want to look, your code should allow an empty array to even be created Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244275 Share on other sites More sharing options...
Maq Posted July 18, 2011 Share Posted July 18, 2011 I would bet there's a better place to implement this error checking. Probably when you are trying to generate the array you can check and handle it properly. Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244279 Share on other sites More sharing options...
mcmuney Posted July 18, 2011 Author Share Posted July 18, 2011 That works, but I also need to place another if/then for endforeach, but I'm getting a parse sytax error: if (is_array($rs_frd)) { endforeach; } Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244280 Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 refer to the edit of my last post.. Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244284 Share on other sites More sharing options...
Adam Posted July 19, 2011 Share Posted July 19, 2011 That works, but I also need to place another if/then for endforeach, but I'm getting a parse sytax error: if (is_array($rs_frd)) { endforeach; } You can't conditionally close a loop. Control structures shouldn't, and thankfully don't, work that way. What you can do is break from the loop. Although I don't see how that would fit into your code anyway? Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244489 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 That works, but I also need to place another if/then for endforeach, but I'm getting a parse sytax error: if (is_array($rs_frd)) { endforeach; } yes I'm not really sure at all what you are trying to accomplish with this code that you posted? Perhaps give a little more information on the matter.. Quote Link to comment https://forums.phpfreaks.com/topic/242271-warning-invalid-argument-supplied-for-foreach/#findComment-1244553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.