Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl or PHP script to parse CSV file

Newbie Knucklehead looks for help

         

Jivago

5:32 pm on Mar 28, 2003 (gmt 0)

10+ Year Member



I'm a fairly new webmaster for a university department. I inherited an absolute mess, page after page of code garbage. After much effort, I'm replacing pages from templates I made using both SSIs and CSS heavily (of which I was clueless, I had to figure it out).

I don't have any databases and I can't have any (I don't control my servers). I have a number of pages with lists of names with various data: phone, email etc. There are three pages for each of a number of the lists. One page has info that is public, another is semi private, and the last is private. Each had more info than the others. It makes for maintanence hassles.

It would be great to have a text file of some sort that a script would pull out what is needed for a particular page. Same data source file for all three pages. Just change the data file and the 3 pages are automatically updated.

Can someone point me in the right direction? My scripting experience is only to pull the crap I inherited out of the pages, clean them up and put them in SSIs. I've found a few simple premade scripts that I've fiddled with and used.

The server is an Apache server, well updated and with most addons, but no databases.

Thanks,

JivaGo

Damian

6:06 pm on Mar 28, 2003 (gmt 0)

10+ Year Member



Hi Jivago, welcome to WebmasterWorld.

Below a sub routine that may help to get you started. $file would be the data file. I haven't tested it as you see it here but I use something similar. It may be quite slow with large files as it gets all the data all the time (I use it in a different situation with small files only)...but that's not hard to change in the example below.

You may want to chose an approach where you get each line as one string and strip something out of them depending on who asks for the script to run. In that case just use a regular expression to take something off each line before you manipulate it further or print it.
I think you can only go from $1 to $9 ..so if you have more then 9 fields for each record, which you probably have, it won't work the way I do it taking apart each line in seperate fields.

sub ShowData {

print qq~
<table border=1 width="95%">
~;

if (open (FILE, $file))
{
@arrayInputData=<FILE>;

foreach $wordline (reverse sort @arrayInputData)
{ if ($wordline =~ /(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)\n/)
{ $item1= $1; $item2 = $2; $item3 = $3; $item4= $4; $item5 = $5; $item6 = $6;}

#bgcolor
$bgcolor++;
if($bgcolor > 1) {$color = "#ebebeb";}
else {$color = "";};

# INSERT if statements to decide what to print.
# Ie..if authorised, print the lot

if ($you_can_see_all) {
print qq~<tr bgcolor="$color">
<td width="20%">$item1</td>
<td width="10%"> $item2</td>
<td width="10%">$item3 item4</td>
<td> &nbsp; $item5 $item6</td>
</tr>~;

}

elsif ($you_can_see_some) {
print qq~
<tr bgcolor="$color">
<td width="20%">$item1</td>
<td width="10%"> $item2</td>
<td width="10%">&nbsp;</td>
<td> &nbsp;</td>
</tr>
~;
}

else {
print qq~
<tr bgcolor="$color">
<td width="20%">$item1</td>
<td width="10%"> &nbsp;</td>
<td width="10%">&nbsp;</td>
<td> &nbsp;</td>
</tr>
~;
}


if ($bgcolor == 2) {$bgcolor = 0;}

close(FILE);

}
}

print qq~
</table>
~;

}

RonPK

6:10 pm on Mar 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this can get you going. Suppose you have a text file with names and numbers, called test.csv and structured like this:

pete;1234567
lizz;9876454

To read the file and display the contents with PHP you could do something like this:


$fp = fopen("test.csv", "r");
while (!feof ($fp)) {
$contents[] = explode(";", fgets($fp, 4096));
}
fclose ($fp);

echo "<table>\n";
foreach($contents as $row) {
echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>\n";
}
echo "</table>";


This code first reads all the stuff in the text file into an array, and then traverses the same array to echo the contents.
Check the superb manual at www.php.net if you want to know more about the functions used, or come back here.

andreasfriedrich

6:35 pm on Mar 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



explode [php.net]ing or split [perldoc.com]ing CSV files will only work in the most simple cases. When the delimter is escaped within a value such code would split there nevertheless.

In Perl [perl.com] Text::ParseWords [perldoc.com] allows you to easily parse CSV files. Using DB_File [theoryx5.uwinnipeg.ca] might be yet a better idea. If you want to store complex structures you can use MLDBM [theoryx5.uwinnipeg.ca].

In PHP [php.net] you should use fgetcsv [php.net] which ignores escaped delimiters as well.

Andreas