Jump to content

Creating array from text file.


fross

Recommended Posts

Hello everyone. I have come seeking your wisdom. I have been trying to figure this code out for over twelve hours, and I cant seem to get it right.

 

What I am trying to accomplish is to take the first and last names and phone numbers from a comma delimitated file and make the first and last name the key and the phone number the value. Each line in the text file is formated as:

 

Lastname,Firstname,Address,City,State,Zip,Areacode,Phonenumber.

 

And my output must be:

 

Lastname, First name  area code - Phone number.

 

And then it needs to be put into a html table.

 

I have tried many different ways to make this happen, but right now my code looks like.

 

<?php

 

$PhoneList = file("phonelist.txt");

 

for ($i=0; $i<count($PhoneList); ++$i)

{

$input = explode("," , $PhoneList[$i]);

$InputName = $input[0] . "," . $input[1];

$Number = $input[6] . "-" . $input[7];

sort($PhoneList);

foreach ($InputName as $output => $Numbers)

echo "<table border='1' width='100%'>\n";

echo "<tr><th>Name</th><th>Phone Number</th></tr>\n";

    echo "<tr><td align='center'>" . htmlentities($output) . "</td><td align='center'>" . htmlentities($Numbers) . "</td></tr>\n";

}

 

echo "</table>\n";

 

?>

 

Any help would be appreciated.

 

 

Link to comment
Share on other sites

Give this a try, not tested

 

<?php

$file = "phonelist.txt";
if (($handle = fopen($file, "r")) !== FALSE)
{
    //Dump data into an array with "lname fname" as the key
    $resultsAry = array();
    while (($line = fgetcsv($file, 1000, ",")) !== FALSE)
    {
        $resultsAry["{$line[1]} {$line[0]}"] = $line;
    }
    fclose($handle);

    //Sort the results by the key
    ksort($resultsAry);

    //Process the sorted results into HTML
    $htmlOutput = '';
    foreach($resultsAry as $data)
    {
        //Parse the values
        list($lname, $fname, $addr, $city, $state, $zip, $acode, $phone) = $data;
        $fullName  = htmlentities("{$fname} {$lname}");
        $fullPhone = htmlentities("{$acode}-{$phone}");
        //Gnerate HTML
        $htmlOutput .= "<tr>\n";
        $htmlOutput .= "<td>{$fullName}</td>\n";
        $htmlOutput .= "<td>{$fullPhone}</td>\n";
        $htmlOutput .= "</tr>\n";
    }
}
else
{
     $htmlOutput .= "<tr><td colspan='2'>Unable to read data file</td></tr>\n";
}

?>
<table border='1' width='100%'>
  <tr><th>Name</th><th>Phone Number</th></tr>
  <?php echo $htmlOutput; ?>
<table>

Link to comment
Share on other sites

Thanks for the reply mjdamato. The code looks great, I can see exactly whats its should do. Thanks for all of the comments in the code as well

 

When I try to run it as-is, the browser hangs at "Waiting for localhost". If I change

 

while (($line = fgetcsv($file, 1000, ",")) !== FALSE)

 

To

 

while (($line = fgets($file, 1000, ",")) !== FALSE)

 

Then the page loads with the table, but no data.

 

I have looked up a few of the unfamiliar functions at php.net but I still cant see what the issue is.

 

Thanks again.

Link to comment
Share on other sites

What version of PHP are you running? Attach the file to the post and I can work on it. I hate trying to resolve issues relating to data when I can't even see the data. If the file as data you don't want exposed, then use a search and replace to replace 0-9 with some other characters. That should remove enough information to make it non-identifiable.

Link to comment
Share on other sites

If you can't use fgetcsv(), then I would revert to using file().

 

I have to go, but this is mostly working. FOr some reason the trim() isn't being applied to the values, which will cause problems with the sorting and display. Plus, the sorting usign ksort() will likely not be case insensitive. Might need to use a different approach

 

<?php

$file = "phonelist.txt";
$linesAry = file($file);

//Dump data into an array with "lname fname" as the key
$resultsAry = array();
foreach ($linesAry as $line)
{
    $dataAry = explode(',', $line);
    array_walk($dataAry, 'trim');
    $resultsAry["{$dataAry[1]} {$dataAry[0]}"] = $dataAry;
}

//Sort the results by the key
ksort($resultsAry);

//Process the sorted results into HTML
$htmlOutput = '';
foreach($resultsAry as $data)
{
    //Parse the values
    list($lname, $fname, $addr, $city, $state, $zip, $acode, $phone) = $data;
    $fullName  = htmlentities("{$fname} {$lname}");
    $fullPhone = htmlentities("{$acode}-{$phone}");
    //Gnerate HTML
    $htmlOutput .= "<tr>\n";
    $htmlOutput .= "<td>{$fullName}</td>\n";
    $htmlOutput .= "<td>{$fullPhone}</td>\n";
    $htmlOutput .= "</tr>\n";
}

?>
<table border='1' width='100%'>
  <tr><th>Name</th><th>Phone Number</th></tr>
  <?php echo $htmlOutput; ?>
<table>

Link to comment
Share on other sites

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.