syazmaya Posted February 12, 2008 Share Posted February 12, 2008 Hello.. i'm a real newbie here in php. i'm currently on a reserch team in my university doing reserch on fleet management positioning. My system is supposed to be able to read a GPS format data and intepret it into a map positioning. I have prepared a dummy navigational data in .txt format to proof my concept. the exerpt is <?php $myFile = "testloc.txt"; $fh = fopen($myFile, 'r'); $Data[1] = fread($fh,7); $Data[2] = fread($fh,26); $Data[3] = fread($fh,4); $Data[4] = fread($fh,11); $Data[5] = fread($fh,9); $Data[6] = fread($fh,11); $Data[7] = fread($fh,11); fclose($fh); for ($i=0; $i<=sizeof($Data); $i++) {echo $Data[$i]."<br>";} ?> the dummy data used is JDY5521 103185.278880 1922.994869 100 parit raja 09:18:05 30/10/2007 0137123456 JDY5521 103192.498912 1934.226031 100 parit raja 09:18:10 30/10/2007 0137123456J DY5521 103219.774591 1964.710614 110 ayer hitam 09:18:15 30/10/2007 0137123456 to those with gps data format knowledge out there, this is not an NMEA format data. i converted it to rescaled version of milimetres. since currently the software i use for the embedded mapping cannot interpret the NMEA (GPRMC) format. the resulting display is somewhat like this on the web for the first line of data (refer to the red colored .txt file above) JDY5521 103185.278880 1922.994869 100 parit raja 09:18:05 30/10/2007 0137123456 these result prove that i can read the first line of my data. however though, my autolooping coding to read the second line data (blue colored .txt data) cannot read the second line of data. i need my system to read the second line of data and so forth automatically every five second. can the experts here help me. really i'm stuck here since i need to be prepared for the exposition fortnightly. thanks and regards. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 12, 2008 Share Posted February 12, 2008 It would be better to separate each item in the lines with commas or some other character you don't expect to be in your data, then you can use the fgetcsv() to read in your data. Ken Quote Link to comment Share on other sites More sharing options...
KrisNz Posted February 12, 2008 Share Posted February 12, 2008 I would do something like this, since it gives more meaningful code and allow you to access the data more easily. It's also more flexible in that it doesn't require each piece of data to have the same length the way your method would. This merges each line with a set of keys to define what each piece of data represents. <?php $keys = array("code","coords1","coords2","coords3","fname","lname","time","date","code2"); //these are just for example of course $tmp = file("test.txt"); //replace with your file name - this reads each line into its own array element $geoData = array(); //create the final array we will store everything in foreach ($tmp as $line) { //loop through our file()'d array $t = explode(" ",$line); //assume that a space delimits each piece of data $geoData[] = array_combine($keys,$t); //combine our keys and data together and store them in our final array } pr($geoData); //output the contents to screen /*this is for convenience*/ function pr($a) { echo "<pre>".print_r($a,1)."</pre>"; } ?> What you also need to consider is how each field is delimited, that is, what character defines the end of a piece of data. I've used whitespace in my example, but say you wanted name to be one field. One way around would be to store name as one value by separating first and last name with an underscore '_'. Another way would be to delimit each piece of data with something other than whitespace. Commonly used characters are ',' '|' and 'þ' to name a few. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.