1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to resolve this "Invalid argument supplied for foreach()" problem?

Discussion in 'PHP' started by _vlada_, Feb 8, 2007.

  1. #1
    Hi

    I have problem with script that shows "Warning: Invalid argument supplied for foreach() in /home/../../test.php on line 17".

    Script working, with showing this error on begining. Any idea what is the problem (since I am not programer, but I need that script :) ) PHP Version on my site is 4.4.3.

    Here is code:
     <?
    $IPaddress=$_SERVER['REMOTE_ADDR'];
    $two_letter_country_code=iptocountry($IPaddress);
    
    if ($two_letter_country_code=="UK"){
         include("This_file_for_UK.html");
          die();
        }else{
         include("This_file_for_others.html");
          die();
        }
    
    function iptocountry($ip) {   
        $numbers = preg_split( "/\./", $ip);   
        include("ip_files/".$numbers[0].".php");
        $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);   
    [B]    foreach($ranges as $key => $value){[/B]
            if($key<=$code){
                if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
                }
        }
        if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
        return $two_letter_country_code;
    }
    ?> 
    Code (markup):
    FYI, very nice script for geotargeting and country identification based on IP address and could be found here:
    phptutorial.info/iptocountry/the_script.html
    and here:
    phptutorial.info/iptocountry/responses.html

    thanks
    SEMrush
     
    _vlada_, Feb 8, 2007 IP
    SEMrush
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    foreach($ranges as $key => $value){

    ranges isn't set
     
    krakjoe, Feb 8, 2007 IP
    _vlada_ likes this.
  3. _vlada_

    _vlada_ Peon

    Messages:
    743
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Any idea how to resolve this? :)
     
    _vlada_, Feb 8, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    change foreach($ranges as $key => $value){ to

    @foreach($ranges as $key => $value){

    it's because not all of those files have items in, and so theres no data to loop over ..... the @ symbol supresses errors for that line
     
    krakjoe, Feb 8, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    You may also check if $ranges is a array or not as,

    if(is_array($ranges) && count($ranges) > 0)
    {
            // your foreach loop goes here
    }
    PHP:
     
    designcode, Feb 8, 2007 IP
    _vlada_ likes this.
  6. _vlada_

    _vlada_ Peon

    Messages:
    743
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thats working! :D

    Thanks people!
     
    _vlada_, Feb 8, 2007 IP
  7. tsironisd

    tsironisd Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes :) Nice Idea thanks...
     
    tsironisd, Mar 13, 2010 IP