bronzemonkey Posted August 31, 2007 Share Posted August 31, 2007 I'm getting PHP Warnings about an "Invalid argument supplied for foreach()". Below is my code and the warning relates to the last section which is trying to loop the relevant array. Could someone give me a hand in fixing the error? Thanks! <?php if($page == '/home.php' || $page == '/about.php' || $path == '/work/'){ $links [0] = "<a href=\"/#.php\">#</a>"; $links [1] = "<a href=\"/#.php\">#</a>"; $links [2] = "<a href=\"/#.php\">#</a>"; $links [3] = "<a href=\"/#.php\">#</a>"; } elseif($page == '/blog.php') { $links [0] = "<a href=\"/#.php\">#</a>"; $links [1] = "<a href=\"/#.php\">#</a>"; } elseif($page == '/contact.php') { $links [0] = "<a href=\"/#.php\">#</a>"; $links [1] = "<a href=\"/#.php\">#</a>"; } ?> <div id="links"> <h3><img src="#" alt="#" /></h3> <ul> <?php foreach ($links as $value) { echo '<li>' . $value . '</li> '; } ?> </ul> </div> Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 their can be times when the links isn't an array.. (ie $page = "") so change <?php foreach ($links as $value) { echo '<li>' . $value . '</li> '; } ?> to <?php if(is_array($links)) { foreach ($links as $value) { echo '<li>' . $value . '</li><br>'; //added <br> to put the break inn } } ?> as a side note try this $links[] = "<a href=\"/#.php\">#</a>"; $links[] = "<a href=\"/#.php\">#</a>"; $links[] = "<a href=\"/#.php\">#</a>"; $links[] = "<a href=\"/#.php\">#</a>"; will all of them as it will build the array (no sense adding the numbers when PHP will do it for you) Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338500 Share on other sites More sharing options...
bronzemonkey Posted August 31, 2007 Author Share Posted August 31, 2007 Thank you, that seems to have done the trick. The php warning was also showing up in SOME instances where I was using the code below: <?php if(strpos($page, 'home.php') !== false) { $links [] = '<a href="#">#</a>'; $links [] = '<a href="#">#</a>'; $links [] = '<a href="#">#</a>'; } elseif(strpos($page, 'about.php') !== false) { $links [] = '<a href="#">#</a>'; $links [] = '<a href="#">#</a>'; $links [] = '<a href="#">#</a>'; } ?> <div id="links"> <h3>#</h3> <ul> <?php foreach ($links as $value) { echo ' <li>' . $value . '</li> '; } ?> </ul> </div> But I was in the process of replacing this kind of code with the code $page == '#' , because I wanted to use OR logic but didn't know how to do that while using strpos. //added <br> to put the break inn I wanted the new line to appear in the source code, not in the browser...I like having the final xhtml properly layed out . Is there a better way of doing it than getting it to echo the necessary spaces/new lines for me? Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338506 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 for the newlines (\n) see below (basically how i would would do it) if you do a var_dump($page); what is returned? <?php if(strpos($page, 'home.php') !== false) { $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; }elseif(strpos($page, 'about.php') !== false){ $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; } echo ' <div id="links">\n'; echo ' <h3>#</h3>\n'; if(is_array($links)) { echo " <ul>\n"; foreach ($links as $value) { echo " <li>' . $value . '</li>\n"; } echo " </ul>\n"; } echo " </div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338520 Share on other sites More sharing options...
bronzemonkey Posted August 31, 2007 Author Share Posted August 31, 2007 Thanks for the tip about newline. if you do a var_dump($page); what is returned? string(9) "/home.php" Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338527 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 OK. cool, maybe this would be a better solution .. <?php switch(strtolower($page)) { default: //break; //not using break here to the default will be home case "/home.php": $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; break; case "/about.php": $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; $links[] = '<a href="#">#</a>'; break; } echo ' <div id="links">\n'; echo ' <h3>#</h3>\n'; echo " <ul>\n"; //don't need to check to see if its an array as the default create the array foreach ($links as $value) { echo " <li>' . $value . '</li>\n"; } echo " </ul>\n"; echo " </div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338533 Share on other sites More sharing options...
bronzemonkey Posted August 31, 2007 Author Share Posted August 31, 2007 Cool thanks for that. I'll give that a try and see what happens. Is it better practice to get php to echo the non-changing xhtml as you have done (rather than leaving it as xhtml as I did)? Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338545 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 for small sections on HTML i use the echo, i wouldn't go as far as saying it better but i find it easier to read anything over about 10 lines of static html i'll probably use the php close tag ?> then reopen it after the html EDIT: just noticed a mistake change echo " <li>' . $value . '</li>\n"; to echo " <li>$value</li>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338552 Share on other sites More sharing options...
bronzemonkey Posted August 31, 2007 Author Share Posted August 31, 2007 Yeah, I found that putting that xhtml into the php did make it easier to read and format. Also noticed that /n needs to be in " " rather than ' ' What was wrong with ' . $value . ' I was given that line of code by another member, and since I wasn't getting any php warnings/errors, I'm curious as to why it is incorrect. Thanks all the tips and cleanup of my code! Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338582 Share on other sites More sharing options...
MadTechie Posted August 31, 2007 Share Posted August 31, 2007 welcome, if solved please click topic solved (bottom left) Quote Link to comment https://forums.phpfreaks.com/topic/67434-solved-invalid-argument-supplied-for-foreach/#findComment-338590 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.