Jump to content

Recommended Posts

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

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.

 

 

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

@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>";
   
?>

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.