Forum Moderators: coopster & phranque

Message Too Old, No Replies

(hopefully a) simple-to-solve flatfile database problem with Perl

         

sarah_anne

10:20 pm on May 21, 2003 (gmt 0)



Hello, everyone! I'm new here and I have a problem (I only just signed up because my normal email address is with <someone> and I had no choice but to use my other half's email account... but couldn't remember my partner's password...!).

Anyway... my problem --

1) I have a flatfile database, in the form of:
$usernum ¦ $rights ¦ $active ¦ $email ¦ $firstname ¦ $lastname ¦ $password ¦$username

So the exact layout with dummy examples would look something like:

<modnote>broke the following lines for sidescroll fix</modnote>

usernum=1¦rights=administrator¦active=yes¦email=sarah@site1.com
¦firstname=Sarah¦lastname=Anne¦password=hello¦username=SarahAnne

usernum=2¦rights=guest¦active=yes¦email=jamesd@site2.com¦
firstname=james¦lastname=duffield¦password=hello¦username=jamesd

And so forth.

2) I would like to be able to read this data so that a user would go to my script (master.cgi) with an indication of a certain user and the information for that user was displayed, kind of a profile. I.e. if a user were to click master.cgi?action=ViewProfile&pro=jamesd

then the information on user "jamesd" would be displayed, i.e. $firstname and $lastname and so on. The ViewProfile is a subroutine and pro= is just telling the script which profile needs to be displayed.

I've tried several things to sort this (what seems to be a pretty simple) problem but no luck. I'm not particularly good with Perl, but here are some of my solutions, none of which work as they are....

(BTW, a big thank you to anyone who can help me with this! :) )

Sarah.

P.S. Sorry for messing up this lovely board with all this code... I just know this is going to get those horrid horizontal bars showing :(

=========================================================
open(FILE, "users.dat");
@indata = <FILE>;
close(FILE);

open(FILE, "users.dat");
foreach $temp (@indata)
{
($usernum, $rights, $active, $email, $firstname, $lastname, $password, $username) = split(/\¦/, $temp);
if ($username eq $pro) {
print FILE "$usernum, $rights, $active, $email, $firstname, $lastname, $username ";
}
else { print FILE "No one by that name!"; }
}
close(FILE);

====================================================

$database='users.dat';
if (&ReadParse())
{
open (DATABASE,$database);
@database_in=<DATABASE>;
foreach $_ (@database_in)
{
($usernum, $rights, $active, $email, $firstname, $lastname, $password, $username)=split(/\¦/);
$temp="$pro";
if ($temp =~ /$in{'searchkeys'}/i)
{
$output=$output." $usernum <br> $username who is $firstname $lastname";
}
}
print "Search Results\n";
print "<b>Search for <i>$in{'searchkeys'}</i> returned:</b><P>\n\n";
if ($output eq "")
{
print "I am sorry, but right now <b>$in{'searchkeys'}</b> is not available. $database='users.dat';
if (&ReadParse())
{
print &PrintHeader;
open (DATABASE,$database);
@database_in=<DATABASE>;
foreach $_ (@database_in)
{
($usernum, $rights, $active, $email, $firstname, $lastname, $password, $username)=split(/\¦/);
$temp="$pro";
if ($temp =~ /$in{'searchkeys'}/i)
{
$output=$output." $usernum <br> $username who is $firstname $lastname";
}
}
print "Search Results $output ";
print "<b>Search for <i>$in{'searchkeys'}</i> returned:</b><P>\n\n";
if ($output eq "")
{
print "I am sorry, but right now <b>$in{'searchkeys'}</b> is not available.\n";
}
else
{
print " ";
print " ";
}
print " ";
}
else
{

print " ";

}\n";
}
else
{
print " ";
print " ";
}
print " ";
}
else
{

print " ";

}

==================================================================

open(file,"data.dat");
@lines=<FP>;
close (FP);
chomp @lines;
foreach $i (@lines)
{
($prod_code, $name, $colour, $size, $price)=split(/\¦/);
$name{$prod_code}=$name;
$colour{$prod_code}=$colour;
$size{$prod_code}=$size;
$price{$prod_code}=$price;
}

$code="100";
print "Product $code is a $name{$code} in colour $colour{$code}, of size $size{$code} costing $price{$code}<Br>";

===================================================================

[edited by: jatar_k at 10:39 pm (utc) on May 21, 2003]
[edit reason] fixed sidescroll and removed specifics [/edit]

Robber

10:18 am on May 22, 2003 (gmt 0)

10+ Year Member



Hi There,

I would suggest reading the info into a hash (associative array) wuth the user name being the key.

You can then extract the username from the query string and search the hash for a match.

Im afraid I dont have time to offer you code snippet though right now.

HTH