Jump to content

Nesting foreach in a function


Recommended Posts

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: MadLittleMods@gmail.com


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 '

					<a href="',$affiliate[href],'"><img src="',$affiliate[image],'"></a>

					<small><a href="',$affiliate[href],'">',$affiliate[href],'</a></small>





// 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(
    "title" => "aff_blah1",
    "image" => "http://aff_blah1.net/images/aff_blah1.png",
    "href" => "http://aff_blah1.net/"

    "title" => "aff_blah2",
    "image" => "http://aff_blah2.net/images/aff_blah2.png",
    "href" => "http://aff_blah2.net/"

    "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]

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.