March 16, 201016 yr can anybody take a look at my code and tell me where i am going wrong? the error is: Warning: Invalid argument supplied for foreach() in /home/ryanmur1/public_html/uni/index.php on line 162 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Loughborough University | Students Union</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <div id="header"> <img src="images/logo.png" /> <span id="greeting"> <?php date_default_timezone_set('GMT'); $date = date('H'); if ($date == "00") echo "<h2>It's a new day! Welcome along and Good Morning.</h2>"; elseif ($date == "01") echo "<h2>Up Early! Good Morning</h2>"; elseif ($date == "02") echo "<h2>Up Early! Good Morning</h2>"; elseif ($date == "03") echo "<h2>Up Early! Good Morning</h2>"; elseif ($date == "04") echo "<h2>Good Morning</h2>"; elseif ($date == "05") echo "<h2>Good Morning</h2>"; elseif ($date == "06") echo "<h2>Good Morning</h2>"; elseif ($date == "07") echo "<h2>Good Morning</h2>"; elseif ($date == "08") echo "<h2>Good Morning</h2>"; elseif ($date == "09") echo "<h2>Good Morning</h2>"; elseif ($date == "10") echo "<h2>Good Morning</h2>"; elseif ($date == "11") echo "<h2>Nearly Midday, What you having for lunch today?</h2>"; elseif ($date == "12") echo "<h2>Its Midday! You had lunch yet? Good Afternoon</h2>"; elseif ($date == "13") echo "<h2>Good Afternoon</h2>"; elseif ($date == "14") echo "<h2>Good Afternoon</h2>"; elseif ($date == "15") echo "<h2>Good Afternoon</h2>"; elseif ($date == "16") echo "<h2>Good Afternoon</h2>"; elseif ($date == "17") echo "<h2>Good Afternoon</h2>"; elseif ($date == "18") echo "<h2>Good Afternoon</h2>"; elseif ($date == "19") echo "<h2>Good Afternoon</h2>"; elseif ($date == "20") echo "<h2>Good Evening</h2>"; elseif ($date == "21") echo "<h2>Good Evening</h2>"; elseif ($date == "22") echo "<h2>Good Evening</h2>"; elseif ($date == "23") echo "<h2>Good Evening</h2>"; else ""; ?> </span> </div> <ul id="menu" style="margin-left:-25px;"> <li><a href="">Home</a></li> <li><a href="">Your Union</a></li> <li><a href="">Halls</a></li> <li><a href="">Sport</a></li> <li><a href="">International</a></li> <li><a href="">The Uni</a></li> <li><a href="">Contact Us</a></li> <li><a href="">Events</a></li> <li><a href="">Get Involved</a></li> <li><a href="">Links</a></li> </ul> <div id="line"></div> <div id="welcome_pic"><div id="play_feature"><font class="play_feature">Demonstration Play Feature</font> <br /> <br /> <div id="your_details"> <font class="play_feature_sub_heading">Your Details</font><br /><br /> <?php /* This file is free software: you can redistribute it and/or modify the code under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. However, the license header, copyright and author credits must not be modified in any form and always be displayed. This class is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @author geoPlugin (gp_support@geoplugin.com) @copyright Copyright geoPlugin (gp_support@geoplugin.com) This file is an example PHP file of the geoplugin class to geolocate IP addresses using the free PHP Webservices of http://www.geoplugin.com/ */ require_once('geoplugin.class.php'); $geoplugin = new geoPlugin(); $geoplugin->locate(); echo "Your IP address is: {$geoplugin->ip} <br />\n". "City: {$geoplugin->city} <br />\n". "Region: {$geoplugin->region} <br />\n". "Country Name: {$geoplugin->countryName} <br />\n". "Country Code: {$geoplugin->countryCode} <br />\n"; ?></div> <div id="your_details"><font class="play_feature_sub_heading">Factoid</font><p>At its zenith in the 19th century, <br />the British Empire stretched over<br> one-fourth of the earth's surface.</p></div> <div id="change_settings"> <font class="play_feature_sub_heading">Change Settings</font><br /><br /> <form method="post" style="margin-left:-5px;"> <p> Select your Language </p><p> <label> <input name="languages[]" type="radio" value="English"/> English </label> <label> <input type="radio" name="languages[]" value="Japanese" /> Japanese </label><label> <input type="radio" name="languages[]" value="Chinese" /> Chinese </label> <br /><br /> <input type="submit" value="Send" name="poll" /> </p></form></div> <div id="flag_counter"> <font class="play_feature_sub_heading">Flag Counter</font><br /><br /> <a href="http://s06.flagcounter.com/more/7ydE"><img src="http://s06.flagcounter.com/count/7ydE/bg=FFFFFF/txt=000000/border=CCCCCC/columns=2/maxflags=25/viewers=Visitors/labels=0/pageviews=1/" alt="free counters"></a></div></div></div> <div id="homepage_text"> <?php foreach ($_POST['languages'] as $language) { switch ($language) { case 'English' : echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; break; case 'Japanese' : echo "<p>ようこそラフバラ学生連合には、ラフバラ大学では学生の大半は第2の故郷。のためのウェブサイトで、もはや、情報閲覧、お問い合わせフォームを使用することを躊躇しないでください。</p> <p>してくださいの'デモ再生機能'は、このウェブサイトからあなたの経験を最大限に活用する。何が起こるのか、明らかに必要がある場合英語を話すことがありますか、または別の日程や場所から見ることができます参照してくださいに使用する言語を変更します。</p> <p>別の便利な機能がある場合は、場所、言語、および、選択した日付に応じて異なる事実を見ることができる'疑似事実'の機能です。</p>"; break; case 'Chinese' : echo "<p>歡迎拉夫堡學生會的第二個家的大部分學生在拉夫堡大學。瀏覽網頁,供各地的信息和再不要猶豫,使用聯繫表格。</p> <p>請充分利用示範播放功能'您的經驗,盡量從這個網站。更改語言來看看會發生什麼,或明顯的需要,您可能不會講英語,也可以從不同的日期或地點。</p> <p>另一個有用的特點是,功能,您可以看到不同的事實而定地點,語言和日期您所選擇。</p>"; break; default: echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; } } ?> </div> <table> <tr><td class="news_events">News</td><td class="news_events">Events</td></tr> <tr><td class="news_events_bullets"><ul><li>News Item 1</li><li>News Item 2</li><li>News Item 3</li></ul></td><td class="news_events_bullets"><ul><li>News Item 1</li><li>News Item 2</li><li>News Item 3</li></ul></td></tr> </table> </div> </body> </html> would be much appreciated , cheers guys
March 16, 201016 yr Are you posting any data to this page, as the code is expecting $_POST['languages'] I presume this is a way to show a different block of text based on the users selection... however, if none is made (ie. posted) it will not work. The block of English doesn't need to be in there twice either - so I have left this in as just the default. If so, try this (although untested): <?php if(!isset($_POST['languages'])) { $_POST['languages'] = array('English'); } foreach ($_POST['languages'] as $language) { switch ($language) { case 'Japanese' : echo "<p>ようこそラフバラ学生連合には、ラフバラ大学では学生の大半は第2の故郷。のためのウェブサイトで、もはや、情報閲覧、お問い合わせフォームを使用することを躊躇しないでください。</p> <p>してくださいの'デモ再生機能'は、このウェブサイトからあなたの経験を最大限に活用する。何が起こるのか、明らかに必要がある場合英語を話すことがありますか、または別の日程や場所から見ることができます参照してくださいに使用する言語を変更します。</p> <p>別の便利な機能がある場合は、場所、言語、および、選択した日付に応じて異なる事実を見ることができる'疑似事実'の機能です。</p>"; break; case 'Chinese' : echo "<p>歡迎拉夫堡學生會的第二個家的大部分學生在拉夫堡大學。瀏覽網頁,供各地的信息和再不要猶豫,使用聯繫表格。</p> <p>請充分利用示範播放功能'您的經驗,盡量從這個網站。更改語言來看看會發生什麼,或明顯的需要,您可能不會講英語,也可以從不同的日期或地點。</p> <p>另一個有用的特點是,功能,您可以看到不同的事實而定地點,語言和日期您所選擇。</p>"; break; default: echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; break; } } ?>
March 16, 201016 yr Actually, what is the foreach needed for? Shouldn't there only be a single language posted? If so, try this: <?php if(!isset($_POST['languages'])) { $_POST['languages'] = 'English'; } switch ($_POST['languages']) { case 'Japanese' : echo "<p>ようこそラフバラ学生連合には、ラフバラ大学では学生の大半は第2の故郷。のためのウェブサイトで、もはや、情報閲覧、お問い合わせフォームを使用することを躊躇しないでください。</p> <p>してくださいの'デモ再生機能'は、このウェブサイトからあなたの経験を最大限に活用する。何が起こるのか、明らかに必要がある場合英語を話すことがありますか、または別の日程や場所から見ることができます参照してくださいに使用する言語を変更します。</p> <p>別の便利な機能がある場合は、場所、言語、および、選択した日付に応じて異なる事実を見ることができる'疑似事実'の機能です。</p>"; break; case 'Chinese' : echo "<p>歡迎拉夫堡學生會的第二個家的大部分學生在拉夫堡大學。瀏覽網頁,供各地的信息和再不要猶豫,使用聯繫表格。</p> <p>請充分利用示範播放功能'您的經驗,盡量從這個網站。更改語言來看看會發生什麼,或明顯的需要,您可能不會講英語,也可以從不同的日期或地點。</p> <p>另一個有用的特點是,功能,您可以看到不同的事實而定地點,語言和日期您所選擇。</p>"; break; default: echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; break; } ?>
March 17, 201016 yr can anybody take a look at my code and tell me where i am going wrong? the error is: Warning: Invalid argument supplied for foreach() in /home/ryanmur1/public_html/uni/index.php on line 162 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Loughborough University | Students Union</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <div id="header"> <img src="images/logo.png" /> <span id="greeting"> <?php date_default_timezone_set('GMT'); $date = date('H'); if ($date == "00") echo "<h2>It's a new day! Welcome along and Good Morning.</h2>"; elseif ($date == "01") echo "<h2>Up Early! Good Morning</h2>"; elseif ($date == "02") echo "<h2>Up Early! Good Morning</h2>"; elseif ($date == "03") echo "<h2>Up Early! Good Morning</h2>"; elseif ($date == "04") echo "<h2>Good Morning</h2>"; elseif ($date == "05") echo "<h2>Good Morning</h2>"; elseif ($date == "06") echo "<h2>Good Morning</h2>"; elseif ($date == "07") echo "<h2>Good Morning</h2>"; elseif ($date == "08") echo "<h2>Good Morning</h2>"; elseif ($date == "09") echo "<h2>Good Morning</h2>"; elseif ($date == "10") echo "<h2>Good Morning</h2>"; elseif ($date == "11") echo "<h2>Nearly Midday, What you having for lunch today?</h2>"; elseif ($date == "12") echo "<h2>Its Midday! You had lunch yet? Good Afternoon</h2>"; elseif ($date == "13") echo "<h2>Good Afternoon</h2>"; elseif ($date == "14") echo "<h2>Good Afternoon</h2>"; elseif ($date == "15") echo "<h2>Good Afternoon</h2>"; elseif ($date == "16") echo "<h2>Good Afternoon</h2>"; elseif ($date == "17") echo "<h2>Good Afternoon</h2>"; elseif ($date == "18") echo "<h2>Good Afternoon</h2>"; elseif ($date == "19") echo "<h2>Good Afternoon</h2>"; elseif ($date == "20") echo "<h2>Good Evening</h2>"; elseif ($date == "21") echo "<h2>Good Evening</h2>"; elseif ($date == "22") echo "<h2>Good Evening</h2>"; elseif ($date == "23") echo "<h2>Good Evening</h2>"; else ""; ?> </span> </div> <ul id="menu" style="margin-left:-25px;"> <li><a href="">Home</a></li> <li><a href="">Your Union</a></li> <li><a href="">Halls</a></li> <li><a href="">Sport</a></li> <li><a href="">International</a></li> <li><a href="">The Uni</a></li> <li><a href="">Contact Us</a></li> <li><a href="">Events</a></li> <li><a href="">Get Involved</a></li> <li><a href="">Links</a></li> </ul> <div id="line"></div> <div id="welcome_pic"><div id="play_feature"><font class="play_feature">Demonstration Play Feature</font> <br /> <br /> <div id="your_details"> <font class="play_feature_sub_heading">Your Details</font><br /><br /> <?php /* This file is free software: you can redistribute it and/or modify the code under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. However, the license header, copyright and author credits must not be modified in any form and always be displayed. This class is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @author geoPlugin (gp_support@geoplugin.com) @copyright Copyright geoPlugin (gp_support@geoplugin.com) This file is an example PHP file of the geoplugin class to geolocate IP addresses using the free PHP Webservices of http://www.geoplugin.com/ */ require_once('geoplugin.class.php'); $geoplugin = new geoPlugin(); $geoplugin->locate(); echo "Your IP address is: {$geoplugin->ip} <br />\n". "City: {$geoplugin->city} <br />\n". "Region: {$geoplugin->region} <br />\n". "Country Name: {$geoplugin->countryName} <br />\n". "Country Code: {$geoplugin->countryCode} <br />\n"; ?></div> <div id="your_details"><font class="play_feature_sub_heading">Factoid</font><p>At its zenith in the 19th century, <br />the British Empire stretched over<br> one-fourth of the earth's surface.</p></div> <div id="change_settings"> <font class="play_feature_sub_heading">Change Settings</font><br /><br /> <form method="post" style="margin-left:-5px;"> <p> Select your Language </p><p> <label> <input name="languages[]" type="radio" value="English"/> English </label> <label> <input type="radio" name="languages[]" value="Japanese" /> Japanese </label><label> <input type="radio" name="languages[]" value="Chinese" /> Chinese </label> <br /><br /> <input type="submit" value="Send" name="poll" /> </p></form></div> <div id="flag_counter"> <font class="play_feature_sub_heading">Flag Counter</font><br /><br /> <a href="http://s06.flagcounter.com/more/7ydE"><img src="http://s06.flagcounter.com/count/7ydE/bg=FFFFFF/txt=000000/border=CCCCCC/columns=2/maxflags=25/viewers=Visitors/labels=0/pageviews=1/" alt="free counters"></a></div></div></div> <div id="homepage_text"> <?php foreach ($_POST['languages'] as $language) { switch ($language) { case 'English' : echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; break; case 'Japanese' : echo "<p>ようこそラフバラ学生連合には、ラフバラ大学では学生の大半は第2の故郷。のためのウェブサイトで、もはや、情報閲覧、お問い合わせフォームを使用することを躊躇しないでください。</p> <p>してくださいの'デモ再生機能'は、このウェブサイトからあなたの経験を最大限に活用する。何が起こるのか、明らかに必要がある場合英語を話すことがありますか、または別の日程や場所から見ることができます参照してくださいに使用する言語を変更します。</p> <p>別の便利な機能がある場合は、場所、言語、および、選択した日付に応じて異なる事実を見ることができる'疑似事実'の機能です。</p>"; break; case 'Chinese' : echo "<p>歡迎拉夫堡學生會的第二個家的大部分學生在拉夫堡大學。瀏覽網頁,供各地的信息和再不要猶豫,使用聯繫表格。</p> <p>請充分利用示範播放功能'您的經驗,盡量從這個網站。更改語言來看看會發生什麼,或明顯的需要,您可能不會講英語,也可以從不同的日期或地點。</p> <p>另一個有用的特點是,功能,您可以看到不同的事實而定地點,語言和日期您所選擇。</p>"; break; default: echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; } } ?> </div> <table> <tr><td class="news_events">News</td><td class="news_events">Events</td></tr> <tr><td class="news_events_bullets"><ul><li>News Item 1</li><li>News Item 2</li><li>News Item 3</li></ul></td><td class="news_events_bullets"><ul><li>News Item 1</li><li>News Item 2</li><li>News Item 3</li></ul></td></tr> </table> </div> </body> </html> would be much appreciated , cheers guys You normally get that error message if the argument of foreach is NOT an array.
March 17, 201016 yr Author Are you posting any data to this page, as the code is expecting $_POST['languages'] I presume this is a way to show a different block of text based on the users selection... however, if none is made (ie. posted) it will not work. The block of English doesn't need to be in there twice either - so I have left this in as just the default. If so, try this (although untested): <?php if(!isset($_POST['languages'])) { $_POST['languages'] = array('English'); } foreach ($_POST['languages'] as $language) { switch ($language) { case 'Japanese' : echo "<p>ようこそラフバラ学生連合には、ラフバラ大学では学生の大半は第2の故郷。のためのウェブサイトで、もはや、情報閲覧、お問い合わせフォームを使用することを躊躇しないでください。</p> <p>してくださいの'デモ再生機能'は、このウェブサイトからあなたの経験を最大限に活用する。何が起こるのか、明らかに必要がある場合英語を話すことがありますか、または別の日程や場所から見ることができます参照してくださいに使用する言語を変更します。</p> <p>別の便利な機能がある場合は、場所、言語、および、選択した日付に応じて異なる事実を見ることができる'疑似事実'の機能です。</p>"; break; case 'Chinese' : echo "<p>歡迎拉夫堡學生會的第二個家的大部分學生在拉夫堡大學。瀏覽網頁,供各地的信息和再不要猶豫,使用聯繫表格。</p> <p>請充分利用示範播放功能'您的經驗,盡量從這個網站。更改語言來看看會發生什麼,或明顯的需要,您可能不會講英語,也可以從不同的日期或地點。</p> <p>另一個有用的特點是,功能,您可以看到不同的事實而定地點,語言和日期您所選擇。</p>"; break; default: echo "<p>Welcome to the Loughborough Students Union, the second home for most of the students at Loughborough University. Browse around the website for anymore information and don't hesitate to use the contact form.</p><p>Please make full use of the 'Demonstration Play Feature' to maximise your experience from this website. Change the language to see what happens or for the obvious need that you might not speak english or you can view from different dates or locations. </p><p>Another useful feature is the 'factoid' feature where you can see different facts depending on the location, language and date you have chosen.</p>"; break; } } ?>
March 17, 201016 yr Author WOW, thankyou sooo much, this was brilliant worked perfectly. its exactly what i needed? soo what was the exact problem that you found? so i kno for next time? thanks soo much. i really appreciate this.
March 17, 201016 yr Pretty well what the error message said! Foreach iterates through an array, if you dont give it an array then it cant iterate and throws the error.
Create an account or sign in to comment