briankopy Posted March 9, 2010 Share Posted March 9, 2010 Ok, I'm a php "noob" so please don't "pwn" me. I have a .txt of conatact info that looks like this... (new line = new entry) Joe , Smith , 123 Fake St. , Springfield , IL. , 60666, 630 , 5882300 Joe , Jones , 123 Fake St. , Springfield , IL. , 60666, 630 , 5882300 Joe , Adams , 123 Fake St. , Springfield , IL. , 60666, 630 , 5882300 I need to output the last name, first name and phone number only in alphabetical order by lastname. Adams, Joe 630-588-2300 Jones, Joe 630-588-2300 Smith, Joe 630-588-2300 any help would be greatly appreciated. Here is what I have so far... (not much) <? $filename = "data.txt"; $fd = fopen ($filename, "r"); $contents = fread ($fd, filesize($filename)) or die("file is empty, please submit data."); fclose ($fd); $fd = fopen ($filename, "r"); $contents = fread ($fd,filesize ($filename)); fclose ($fd); $fp = fopen($filename, "r") or die("failure to open."); while(!feof($fp)) { $line = fgets($fp); list($namefirst, $namelast, $streetaddress, $city, $state, $zip, $areacode, $phone) = explode(",", $line); ?> //thanks again Quote Link to comment Share on other sites More sharing options...
aeroswat Posted March 9, 2010 Share Posted March 9, 2010 i'm wrong Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 9, 2010 Share Posted March 9, 2010 Did you make and manage the text file? HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
nafetski Posted March 9, 2010 Share Posted March 9, 2010 What you're asking isn't an easy question...nobody will 'pwn' you here So, what you want to do is parse that file as a CSV...which, php has a function for that! http://php.net/manual/en/function.fgetcsv.php What you would do is while you are iterating through the file, store the results into an array. Once you have stored the results - you can then use some of PHP's array functions to sort it as you please. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 10, 2010 Share Posted March 10, 2010 Really not much to it. $lines=file("./data.txt"); asort($lines); foreach ($lines as $line) { //do what you want here list($first, $last, $address, $city, $state, $zip, $areacode, $phone) = explode(",", $line); $pre= substr("$phone", 0, 3);//get phone prefix $suf= substr("$phone", 3, 7);// get phone suffix $pn="$pre-$suf"; echo "$last $first $areacode $pn <br>"; } HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 10, 2010 Share Posted March 10, 2010 @teamatomic: Not sure where you were going with that, but it doesn't sort by last name. The asort() is sorting the lines as an entire sting and the string begins with the first name. The OP wants to sort by last name. @OP: This is tested: Just read the data into a multidimensional array and then create a custom function to use with usort() to sort in any manner you wish. The example below will sort by last name and then by first name (if the last names are the same). You can add as many sub sorts as you wish. <?php $dataFile = "data.csv"; //The data file to read $contactData = array(); //Array to hold the results //Array of keys to apply to each line of data $keys = array('first', 'last', 'street', 'city', 'state', 'zip', 'area', 'phone'); //Read CSV and put into multi-dimensional array if (($handle = fopen($dataFile, 'r')) !== FALSE) { while (($contactLine = fgetcsv($handle, 1000, ",")) !== FALSE) { //Trim values and add to output array $values = array_map("trim", $contactLine); $contactData[] = array_combine($keys, $values); } fclose($handle); } //Create function for sorting output array function sortContacts($a, $b) { //Sort by last name if different if ($a['last']!=$b['last']) { return strcmp($a['last'], $b['last']); } //Sort by first name if different if ($a['first']!=$b['first']) { return strcmp($a['first'], $b['first']); } return 0; } //Sort the array using custom function above usort($contactData, 'sortContacts'); //Ouput the results echo "<pre>"; print_r($contactData); echo "</pre>"; ?> Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 10, 2010 Share Posted March 10, 2010 @mjdamato Yes, it does. guess I didnt think deep enough. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 10, 2010 Share Posted March 10, 2010 Now its fixed and it is sorted all the way across the whole line by last name then sorted by first and city state. Sorry for the first brain dead post. $array=array(); $lines=file("./test.txt"); $i=0; foreach ($lines as $line) { list($first, $last, $street, $city, $state, $zip, $area, $phone)=explode(",",$line); $cl= "$last,$first,$street,$city,$state,$zip,$area,$phone"; $array[$i]=$cl; $i++; } asort($array); foreach ($array as $line) { //do what you want here list($last, $first, $address, $city, $state, $zip, $areacode, $phone) = explode(",", $line); $pre= substr("$phone", 0, 3);//get phone prefix $suf= substr("$phone", 3, 7);// get phone suffix $pn="$pre-$suf"; echo "$last $first $areacode $pn <br>"; } HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
briankopy Posted March 10, 2010 Author Share Posted March 10, 2010 Thanks a lot man. Seems to be working perfectly, I did have an issue with the text file having a blank line at the end but as soon as I got rid of that smooth sailing... Thanks again for the quick response. 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.