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
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> $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%"> </td>
<td> </td>
</tr>
~;
}
else {
print qq~
<tr bgcolor="$color">
<td width="20%">$item1</td>
<td width="10%"> </td>
<td width="10%"> </td>
<td> </td>
</tr>
~;
}
if ($bgcolor == 2) {$bgcolor = 0;}
close(FILE);
}
}
print qq~
</table>
~;
}
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>";
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