...

View Full Version : How do I terminate this for loop and echo the result



jeddi
07-09-2009, 02:22 PM
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 :o

This is the script:


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

Fou-Lu
07-09-2009, 02:30 PM
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:


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



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.

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.

jeddi
07-09-2009, 02:39 PM
Well I solved the test problem by doing this:


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



$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 ;)

jeddi
07-09-2009, 02:41 PM
Yippee

ok - onward and upward !

Fou-Lu
07-09-2009, 02:43 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum