MadLittleMods Posted December 28, 2010 Share Posted December 28, 2010 I think this is the right place to post this but please move if needed as this is my first time on this site. You can also contact me through AIM: [email protected] Okay so I have a multidimensional array that is giving me an error when I put it in a function: ERROR: Warning: Invalid argument supplied for foreach().. line 123 LINE 123: foreach ( $affiliates as $affiliate ) This does work when in one file test2.php which is attached to this post... Here is my whole function: // affiliate content function start: function affiliates_content() { global $context, $settings, $options, $scripturl, $txt; foreach ( $affiliates as $affiliate ) { echo ' <table> <tr> <td> <a href="',$affiliate[href],'"><img src="',$affiliate[image],'"></a> </td> <td> ',$affiliate[title],' <br> <small><a href="',$affiliate[href],'">',$affiliate[href],'</a></small> </td> </tr> </table> <br> '; } } // affiliate content function end Here is my whole array which is located on another file (subs.php - smf) // affiliates array for affiliates page $affiliates = array( array( "title" => "aff_blah1", "image" => "http://aff_blah1.net/images/aff_blah1.png", "href" => "http://aff_blah1.net/" ), array( "title" => "aff_blah2", "image" => "http://aff_blah2.net/images/aff_blah2.png", "href" => "http://aff_blah2.net/" ), array( "title" => "aff_blah3", "image" => "http://aff_blah3.net/images/aff_blah3.png", "href" => "http://aff_blah3.net/" ), ); // affiliates array end Does anyone know how to fix this problem? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/ Share on other sites More sharing options...
trq Posted December 28, 2010 Share Posted December 28, 2010 $affiliates doesn't exist within your function. Also, all the global's declared are never used, remove them. Using the global keyword completely breaks part of what functions are best at (abstracting away code). Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152014 Share on other sites More sharing options...
MadLittleMods Posted December 28, 2010 Author Share Posted December 28, 2010 $affiliates doesn't exist within your function. Also, all the global's declared are never used, remove them. Using the global keyword completely breaks part of what functions are best at (abstracting away code). Thanks for moving Globals removed. nothing... $affiliates does exist and i know the file its on gets called on because the nav bar is also in it and that appears on the page. How do i get it to "see" it? Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152020 Share on other sites More sharing options...
trq Posted December 28, 2010 Share Posted December 28, 2010 I'm not hinting at the fact that $affiliates doesn't exist, I'm telling you it is fact. You need to pass it to your function. function affiliates_content($affiliates) { // rest of code } Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152022 Share on other sites More sharing options...
MadLittleMods Posted December 28, 2010 Author Share Posted December 28, 2010 function affiliates_content($affiliates) { // rest of code } gives me another error: ERROR: Missing argument 1 for affiliates_content(), called in /*/*/*/*/affiliates/index.php on line 72 and defined in /*/*/*/*/affiliates/index.php on line 118 LINE 72: ',affiliates_content(),' ^ in the another function called just "affiliates" LINE 118: function affiliates_content($affiliates) Sorry got to go to bed - Will be checking tommorrow - Thanks for all the help so far! Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152024 Share on other sites More sharing options...
trq Posted December 28, 2010 Share Posted December 28, 2010 You then need to pass it to the function when you call it. affiliates_content($affiliates); This is all covered in the manual and should be learned as part of the basics. functions. Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152025 Share on other sites More sharing options...
MadLittleMods Posted December 28, 2010 Author Share Posted December 28, 2010 After that the error goes away and I am left with this one again.. ERROR: Invalid argument supplied for foreach() in ... line 123 Bed time - Thanks for the help so far Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152026 Share on other sites More sharing options...
trq Posted December 28, 2010 Share Posted December 28, 2010 Modify your function to this and show us the output. function affiliates_content($affiliates) { echo '<pre>'; print_r($affiliates); echo '</pre>'; } Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152030 Share on other sites More sharing options...
MadLittleMods Posted December 28, 2010 Author Share Posted December 28, 2010 No output. EDIT: Figured it out! I just had to "pass" the variable to all of the functions that it is nested and called in. Thanks so much for all this great help and moving my thread to the proper position! Quote Link to comment https://forums.phpfreaks.com/topic/222785-nesting-foreach-in-a-function/#findComment-1152267 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.