Solved

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

Posted on 2013-01-10
5
461 Views
Last Modified: 2013-01-11
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
Comment
Question by:dkauger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 500 total points
ID: 38766069
$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
 

Author Comment

by:dkauger
ID: 38767632
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38769056
oops, there is a typo, please replace

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

by

    print '<tr>'.$td . $lastname . '</td>';
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 38769070
// your grep line looks somehow like (assuming $name proper set):

if(!preg_match('/'.$name.'/i',implode(' ',$data))) { continue; };
0
 

Author Comment

by:dkauger
ID: 38769310
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question