www.webdeveloper.com
Results 1 to 7 of 7

Thread: Help! Warning: Invalid argument supplied for foreach()

Hybrid View

  1. #1
    Join Date
    Aug 2013
    Posts
    1

    Help! Warning: Invalid argument supplied for foreach()

    I had a developer add some code to display shops (author names) in the header menu of my theme.

    It all worked fine in earlier versions of Wordpress (3.5.1.), but since updating to 3.5.2 (soon to be 3.6) and updating my theme. I am getting this error:

    Warning: Invalid argument supplied for foreach() in....

    Here is the code the error is relating to:
    PHP Code:
    function ik_print_shops() {
        global 
    $wpdb;
        
    $shop_list '';

        foreach (
    $shop_data as $key => $shop ) :
            
    $shop_letter substr(strtoupper($shop['shop']),0,1);
            if(
    $key == || ($shop_letter $last && $key >= $last_index)) {
                
    $last_index $key+$block_size-1;
                if (
    $last_index $number_of_shops 1$last_index $number_of_shops 1;
                
    $user2 $shop_data[$last_index];
                
    $last substr(strtoupper($user2['shop']),0,1);
                
    $block_count 0;
            }
            if(
    $block_count == 0) {
                if(
    $shop_letter <> $first$shop_list .=  '</ul></li>';
                
    $shop_list .= '<li class=""><a class="ik_sub_list" href="#">'."Shops $shop_letter to $last</a>"// --- $key -> $last_index
                
    $shop_list .= '<ul class="children">';
            }
               
    $shop_list .=  '<li class="page-item"><a class="sf-with-ul" href="'.site_url().'/author/'.$shop['login'].'">' $shop['shop']. ' ('.$shop['adscount'].')'.'</a></li>';

               
    $num++;
               
    $block_count++;
        endforeach; 
    // end the users loop.

        
    $shop_list .=  '<li class="list-end"></li>';
        
    $shop_list .=  '</ul></li>';
        
    $shop_list .=  '</ul><! -- /ik-nav-shops -->';
        
    $shop_list .= '</div><!-- /adv_shops -->';
        
    $shop_list .=  '</li><! -- /shops_menu -->';
        
    $shop_list .=  '</ul><! -- /menu sf-js-enabled -->';

        return 
    $shop_list;
        }
    }
    add_shortcode'ik_menu_item_shops''ik_print_shops' ); 
    Appreciate any help given

    Thank you
    Sarah

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    This error means that $shop_data is either not an array or not defined.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,414
    IMHO $shop_data was either a global $shop_data; along with $wpdb or part of an argument to the function ik_print_shops($shop_data)

    Suggest you look at the call to the function to see if the call is passed a parameter, if so then add the $shop_data to the function parameter list otherwise make it an addition to the global parameter line.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    All variables (except the so-called super-global variables such as $_POST or $_SERVER) within a function definition are local to that function, unless you define them a global (which is generally frowned upon). So as currently coded, your ik_print_shops() function has no idea what $shop_data is. You may want to define it as a function parameter and then pass it in when you call the function, perhaps?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,414
    That depend on if the function is passed a parameter or not, frowned upon or not, globals do esist. So without knowing the rest of the script, the option is open to the OP to either make the variable a global or a parameter.

    Unless the OP posts more code, we shall never know.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    Well, we know that the function definition has no parameter list, so it's pretty safe to say they're not being passed in as parameters, nor are they being declared as global within the function, therefore while inside the function they are undefined -- unless, as you say, maybe it's supposed to be something in $wpdb. In any case, it's now up to the OP to determine where $shop_data is supposed to come from, and if it needs to be passed as a parameter or otherwise made accessible to the funtion.

    IMO, "global" should be a last resort (and I've very occasionally used it when getting something fixed now was more important than getting it fixed correctly. As prolific PHP author Larry Ullman said:

    Global variables are "bad" for a couple of reasons. For one, they increase the possibility of bugs because a global variable's value can be changed anywhere. Second, they reflect bad design. Ideally functions should be like black boxes. They should work without knowledge of what's going on outside of them. At the very least, this makes the functions more reusable. When you rely upon global variables, your functions lose their portability.

    Although I'm not 100%, universally against using global variables, the fact is that they can easily be avoided by passing the values as parameters.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,414
    That may be true for large projects that will be worked on by many people who will indiscriminately use variables and naming can clash but a small projects where one person is maintaining, it is an immediate fix for a problem.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles