fross Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 10, 2011 Share Posted August 10, 2011 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> Quote Link to comment Share on other sites More sharing options...
fross Posted August 10, 2011 Author Share Posted August 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
fross Posted August 10, 2011 Author Share Posted August 10, 2011 Here you go, here is the showlist.php modified with your code, and the .txt file im working with. The PHP version I have running is 5.3.6 [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 10, 2011 Share Posted August 10, 2011 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> Quote Link to comment Share on other sites More sharing options...
fross Posted August 11, 2011 Author Share Posted August 11, 2011 Thanks man, I got it working and you are forever my hero We can mark this thread as solved! (I cant seem to find the option if I can do that) 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.