• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

I need to read a .csv file line by line and assign selected fields to variables.

I am currently using .cgi files using Korn Shell (#!/bin/ksh) for file manipulation.
I'm reading a line in at a time and using nawk to send back selected fields and assigning to variables which I plug into my html.  I need to replace the ksh code used in the files with something else such as php and perform the same function.  I experimented with the fgetcsv function and was unable to get it to retrieve the data.  Below is a snippet of the code I'm currently using and would love to get a php alternative that actually works.
The code is using a word/name entered by the user, searching through the file and returning selected fields in the matching record which are assigned variables and displayed in a html table.

     exec 9< $csv
      while
           read -u9 record
      do
                  found=$(echo $record|nawk -F, '{print $0}' | grep -ic "$name" )
                 if [[ $found >0 ]];then
                       lastname=$( echo $record | nawk -F, '{print $2}')
                       firstname=$(  echo $record  | nawk -F, '{print $3}')
                       phone=$(  echo $record  | nawk -F, '{print $10}')
                       if [[ $phone == "" ]];then
                          phone="Not On Record"
                       fi
                       email=$(  echo $record  | nawk -F, '{print $11}')    
                       if [[ $email == "" ]];then
                          email="Not On Record"
                       fi      
                   
                   
                    print "<td align=\"center\" bgcolor=\"#FFFFFF\"  style=\"border: 5px outset #000060\" >$lastname</td>"
                    print "<td align=\"center\" bgcolor=\"#FFFFFF\"  style=\"border: 5px outset #000060\" >$firstname</td>"
                    print "<td align=\"center\" bgcolor=\"#FFFFFF\"  style=\"border: 5px outset #000060\" >$phone</td>"
                    print "<td align=\"center\" bgcolor=\"#FFFFFF\"  style=\"border: 5px outset #000060\" >$email</td></tr>"                  

                  fi
 
               
    done
                 
                 
exec 9<&-
0
dkauger
Asked:
dkauger
  • 3
  • 2
2 Solutions
 
ahoffmannCommented:
$fh=fopen('your.csv','r') or die('**ERR**');
while ($data = fgetcsv($fh,4242,',')) {
   // add your grep match here
    $lastname = $data[2];
    $firstname = $data[3];
    $phone = $data[10];
    $email = $data[11];
  // add your checks for empty values here
    $td  = '<td align="center" bgcolor="#FFFFFF" style="border:5px outset #000060">';
    print "<tr>'.$td . $lastname . '</td>';
    print "$td . $firstname . '</td>';
    print "$td . $phone . '</td>';
    print "$td . $email . '</td></tr>';
}
fclose($fh);
// to be improved in many ways
0
 
dkaugerAuthor Commented:
I tried plugging the code into my existing file with the test parameters and it crashes with a "Parse error: syntax error, unexpected T_VARIABLE" in the   print "$td . $firstname . '</td>';  line.
Assuming it was due to the surrounding code, I created a test file which also crashed with the following:

Parse error: syntax error, unexpected $end  at the last line of code.  This is my test file:

<html>
<body>
<table>

<?php    
$fh=fopen('members.csv','r') or die('**ERR**');
while ($data = fgetcsv($fh,4242,',')) {
       $lastname = $data[2];
       $firstname = $data[3];
       $phone = $data[10];
       $email = $data[11];
 
    $td  = '<td align="center" bgcolor="#FFFFFF" style="border:5px outset #000060">';
    print "<tr>'.$td . $lastname . '</td>';
    print $td . $firstname . '</td>';
    print $td . $phone . '</td>';
    print $td . $email . '</td></tr>';
 
}
fclose($fh);
?>
</table>
</body>
</html>

I assumed that the above code would read each line and print the selected fields.
0
 
ahoffmannCommented:
oops, there is a typo, please replace

    print "<tr>'.$td . $lastname . '</td>';

by

    print '<tr>'.$td . $lastname . '</td>';
0
 
ahoffmannCommented:
// your grep line looks somehow like (assuming $name proper set):

if(!preg_match('/'.$name.'/i',implode(' ',$data))) { continue; };
0
 
dkaugerAuthor Commented:
That did it!  I needed the "preg_match" to do the test for the existence of the searched for word, the grep function only returned "array" in the $data value.  I also needed to shift the array elements by one due to the first field being placed in element 0.
A little practice with this code should solve my search and display problem.
Thanks...
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now