PDA

View Full Version : Data file reading correctly


dips_007
03-22-2004, 07:57 PM
I am reading a datafile which is space separated it looks likes this

City County Latitude Longitude Easting Northing

NEWLYN CL -19980 -19980 146 28
NEWMALDEN LD -960 -960 520 168
NEWMARKET SF 1440 1440 563 262
NEWMILLS DY -7200 -7200 400 385
NEWMILNS SH -15600 -15600 253 636
NEWMILTON HE -5940 -5940 424 94
NEWNHAM GR -8820 -8820 368 211
NEWPORT EX 780 780 552 234
NEWPORT GW -10800 -10800 330 187
NEWPORT IW -4680 -4680 449 89
NEWPORT PK -17400 -17400 205 239
NEWPORT SE -8519 -8519 375 318

the problem i have is if the user enters a town the find the first town that matchs but if the user enters a county as well then should try to find the match the problem is that it always stops at the first match eg NEWPOERT


/* open the data file */

$fn = fopen("/townsx","r");

/* now to read the files record by record */

while($rec = fgets($fn,256))
{

/* split each record into fields (: is separator) */

$elem = split(":",$rec);

if($elem[0] == $town1 && $elem[1] == $county1)
{
$g1 = 1;
$x1 = $elem[4];
$y1 = $elem[5];

}
else if($elem[0] == $town1)
{
$g1 = 1;
$x1 = $elem[4];
$y1 = $elem[5];

}

if($elem[0] == $town2 && $elem[1] == $county2)
{
$g2 = 1;
$x2 = $elem[4];
$y2 = $elem[5];
}
else if($elem[0] == $town2)
{
$g2 = 1;
$x2 = $elem[4];
$y2 = $elem[5];
}

if($elem[0] == $town3 && $elem[1] == $county3)
{
$g3 = 1;
$x3 = $elem[4];
$y3 = $elem[5];
}
else if($elem[0] == $town3)
{
$g3 = 1;
$x3 = $elem[4];
$y3 = $elem[5];
}


/* test whether towns have been found */

if( $g1 == 1 && $g2 == 1&& $g3 == 1) break;

inkedmn
03-22-2004, 10:35 PM
Note: Please wrap you code in [ code ] and [ /code ] tags. This will allow your code to be properly indented when it's shown.

:)

bdl
03-23-2004, 02:02 AM
Try this version, see if this doesn't do what you want...

// array of counties => towns to find
$find_array = array('GW'=>'NEWPORT','SE'=>'NEWPORT','CL'=>'NEWLYN');
$file = '/townsx';
if ( $fp = fopen($file, 'r') )
{
while ( !feof($fp) )
{
$line = fgets($fp);

foreach ( $find_array AS $county => $town )
{
if ( strpos($line, $county) !== FALSE )
{
// the county matches, now look for the correct town
if ( strpos($line, $town) !== FALSE )
{
// the county and town both match
// do whatever you need done here
echo '' .$line. '
';
break;
}
}
}
}
}
else
{
echo "Can't open file $file
";
}


Searches through the file line by line, if it finds a county match, looks for the town. If it finds a town match, does whatever you need done; there I simply display the found values, but you could increment a counter or assign some other value to a variable. You seem to be looking for three specific values at once, so I simply took three pairs of counties / towns and stored them in an array. HTH

dips_007
03-23-2004, 11:47 AM
What is the folloing line
becuase the file i have has alot of data in it i can not write all the cities in this line.
The way i hade it sort of worked but only run the else part of the statement which ment used the first newoport for example.


$find_array = array('GW'=>'NEWPORT','SE'=>'NEWPORT','CL'=>'NEWLYN');

bdl
03-23-2004, 02:55 PM
Originally posted by dips_007
What is the folloing line
becuase the file i have has alot of data in it i can not write all the cities in this line.
The way i hade it sort of worked but only run the else part of the statement which ment used the first newoport for example.


$find_array = array('GW'=>'NEWPORT','SE'=>'NEWPORT','CL'=>'NEWLYN');


That line simply initializes an array of county / town pairs to search for. The code I posted still utilizes your same file 'townsx' with all the same data. I'm uncertain as to where you get the variables $town1 / $county1 etc so I used an array for this part. Think of it this way - in my example, the array key $find_array['GW'] is equal to your variable $county1. If you want you can substitute an array with your values, like

$find_array = array($county1 => $town1, $county2 => $town2, $county3 => $town3);

dips_007
03-23-2004, 04:08 PM
Town 1,2 and 3 and county 1 , 2 and 3 are from the user on a form

bdl
03-23-2004, 09:35 PM
Originally posted by dips_007
Town 1,2 and 3 and county 1 , 2 and 3 are from the user on a form

Right, ok so you can simply feed them into an array as I've shown. Again, creating the array and stepping through the file a line at a time until a match is made for each key => value pair within the array is the easiest method I can think of.