newbtophp Posted August 22, 2009 Share Posted August 22, 2009 Im using the Yahoo api, for my search script. But sometimes I search something I get: Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/search.php on line 240 Heres the code which is causing the problem: <? if ($rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")) { } // Go through the list powered by the search engine listed and get the data from each <item> foreach($rs['items'] as $item) { //this is line 240 // Get the title of result $title = $item['title']; // Get the description of the result $description = $item['description']; // Get the link eg amazon.com $urllink = $item['guid']; include "templates/result.php"; echo "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 22, 2009 Share Posted August 22, 2009 foreach() expects an array. When you don't supply it with an array you get that error message. Your code has an if(...){...} conditional statement to test the results of the $rs = $rss->get() function call, but you have put the ending } on the line right after the line with the if() test. You should have put the } after your code that processes the data so that the code will only be executed when there is data to process. You should also have an else {} clause as part of the if(){} statement to output a message indicating that the search did not match anything. Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/#findComment-903958 Share on other sites More sharing options...
newbtophp Posted August 22, 2009 Author Share Posted August 22, 2009 Can you post some code please?, so I can further understand. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/#findComment-903962 Share on other sites More sharing options...
PFMaBiSmAd Posted August 22, 2009 Share Posted August 22, 2009 if ($rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")) { // code to execute when $rss->get() returns something } else { // code to execute when $rss->get() does not return anything } Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/#findComment-903977 Share on other sites More sharing options...
newbtophp Posted August 22, 2009 Author Share Posted August 22, 2009 Ok I've updated the code but I get an error: Parse error: syntax error, unexpected T_ELSE in /home/mysite/public_html/search.php on line 261 <?php if ($rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")) { } // Go through the list powered by the search engine listed and get the data from each <item> foreach($rs['items'] as $item) { //this is line 240 // Get the title of result $title = $item['title']; // Get the description of the result $description = $item['description']; // Get the link eg amazon.com $urllink = $item['guid']; include "templates/result.php"; } else { //Line 261 // code to execute when $rss->get() does not return anything echo "Error!\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/#findComment-903979 Share on other sites More sharing options...
Daniel0 Posted August 22, 2009 Share Posted August 22, 2009 You forgot to close the if (or the foreach depending on how you look at it). Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/#findComment-903981 Share on other sites More sharing options...
newbtophp Posted August 22, 2009 Author Share Posted August 22, 2009 Thanks PFMaBiSmAd & Danny. Solved! Quote Link to comment https://forums.phpfreaks.com/topic/171397-solved-warning-invalid-argument-supplied-for-foreach-in/#findComment-903982 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.