[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-01-10
5
Medium Priority
?
478 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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…

649 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