Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How do I terminate this for loop and echo the result

    Hi,
    I am testing my script to locate
    the ip address from a look up file.

    It seems to work OK as the script stops when it finds
    the correct row - but I can not echo the final result

    This is the script:

    PHP Code:
    <?php

    $ip 
    "1094913047";

    $geo_arr file('GeoIP_sm.csv');
        
    for(
    $i=0$i<count($geo_arr); $i++) {
        
    $arrayOfLine explode(','$geo_arr[$i]);
        if(
    $ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
        
    $countryName $arrayOfLine[2];
        return (
    $countryName);
        }
        else { 
           echo 
    "<br>Searching: line $i - $arrayOfLine[0]:$arrayOfLine[1]:$arrayOfLine[2]";
          }     
    }
            
    echo 
    "<BR> <B>Country: </B>".$countryName;
    ?>
    For some reason the display stops like this:

    Searching: line 11436 - 1094912248:1094912775:"United States"
    Searching: line 11437 - 1094912776:1094912783:"Puerto Rico"
    Searching: line 11438 - 1094912784:1094913039:"United States"

    Looks like the following line contains the
    number 1094913047 in its range.

    But why doesn't it display it.

    I must be using the return incorrectly ?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You're returning from out of a function scope, so that won't work. You will never enter the echo in there.
    What you want is keyword break, or even better, a sentinal value to add to you're if control:
    PHP Code:
    for($i=0$i<count($geo_arr); $i++) {
        
    $arrayOfLine explode(','$geo_arr[$i]);
        if(
    $ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
        
    $countryName $arrayOfLine[2];
        break;
        }
        else { 
           echo 
    "<br>Searching: line $i - $arrayOfLine[0]:$arrayOfLine[1]:$arrayOfLine[2]";
          }     
    }
    // Or, even better:
    $iCount count($geo_arr);
    $bContinue true;
    for (
    $i 0$i $iCount && $bContinue; ++$i)
    {
        
    $arrayOfLine explode(','$geo_arr[$i]);
        if(
    $ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
        
    $countryName $arrayOfLine[2];
        
    $bContinue false;
        }
        else { 
           echo 
    "<br>Searching: line $i - $arrayOfLine[0]:$arrayOfLine[1]:$arrayOfLine[2]";
          }     

    Edit:
    BTW, you shouldn't check you're IP addresses this way. It will result in the incorrect value since you can have combination that match that don't actually match. For example: 10.17.252.9 = 101.72.5.29 when they don't actually match. You need to check each octlet on the ip address one by one.
    Edit:
    Sorry sorry I shouldn't really post when I first wake up - that's not what you're doing. I still wouldn't check this way, simply because string comparisons may not work for what you're wanting in all situations, but it looks like thats how the file is provided for you.

    Last edited by Fou-Lu; 07-09-2009 at 01:38 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Well I solved the test problem by doing this:

    PHP Code:
    for($i=0$i<count($geo_arr); $i++) {
        
    $arrayOfLine explode(','$geo_arr[$i]);
        if(
    $ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
            
    $countryName $arrayOfLine[2];
            echo 
    "<BR> <B>Country: </B>".$countryName;
            break;
            }
        else { 
                   echo 
    "<br>Searching: line $i - $arrayOfLine[0]:$arrayOfLine[1]:$arrayOfLine[2]";
                  }     

    But it doesn't really solve it because
    I want to use that $countryName later on

    What if I declare the variable before the for loop ?

    Will it get up dated in the loop ?

    EG

    PHP Code:
    $countryName "test";

    for(
    $i=0$i<count($geo_arr); $i++) {
        
    $arrayOfLine explode(','$geo_arr[$i]);
        if(
    $ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) {
            
    $countryName $arrayOfLine[2];
            echo 
    "<BR> <B>Country: </B>".$countryName;
            break;
            }
        else { 
                   echo 
    "<br>Searching: line $i - $arrayOfLine[0]:$arrayOfLine[1]:$arrayOfLine[2]";
                  }     
    }

    echo 
    "<BR> <B>Country: </B>".$countryName
    Guess I better test that one

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Yippee

    ok - onward and upward !

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Although you should predeclare it, the only time it won't exist is when no match is found. When you break or preterminate you're loop the last value for $countryName is the one that will be used. If its not set or empty, than it would mean that the matching failed.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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