Gossamer Forum
Home : Products : DBMan : Customization :

Two requests.....

Quote Reply
Two requests.....
Firstly I should say that this is the best support forum I have seen. Ever.

JPDeni, Alex, anyone.....

Request 1.
I need a subroutine to query the database so that when the user logs in, the home page will say something like " Welcome user_name, you have x number of validated records in the database." So basically the script will check the logged in user vs. the userid field and the validated field in all records and let the user know how many validated records he has.

Request 2.
I want to validate records being added by field values. If field 1=1 and field 2=1 then add the record, however if field 1=0 and field 2=1 then send the user to the failure screen with the appropriate message.

PS. I really admire the way this script is structured. It makes understanding it's functional areas relatively easy. I'm new to perl, but I think I've got a great launch pad with DBman and this forum.

Thanks Alex !!!
Quote Reply
Re: Two requests..... In reply to
The first thing would be accomplished in html_home.

Code:
$in{'UserID'} = $db_userid; # change "UserID" to the name of your user id field
$in{'Validated'} = "Yes"; # or whatever indicates a validated record
my ($status, @hits) = &query("view");
print "Welcome, $db_userid. You have $db_total_hits validated records in the database.";

With your second thing, you could add something to sub validate_record in db.cgi. After the duplicate key check -- it starts with

if ($in{'add_record'})

and ends with

close DB;
}

-- add

Code:
unless (($in{'field1'} == 1) && $in{'field2'} == 1)) {
push(@input_err, "[your error message]");
}

I agree that the structure of the code is superb. There is no way I could have learned so much Perl without such a wonderful example to show me how it should be done.



------------------
JPD



Quote Reply
Re: Two requests..... In reply to
JPDeni,

Thanks !!!
I stuck in the code for the $db_total_hits without a problem, and even added

if ($db_total_hits > "0" ){
print qq| "Welcome, $db_userid. You have $db_total_hits validated records etc | ;
}

so that it doesn't print "You have 0 validated records" if that is the case. I would like to expand on this though, here's the idea :-

I would also like to show how many(total) records in the database are validated, how many have a field value of "Yes" for {'Popular'}, etc. As I add more fields I would like to be able to add more info of this type to the homepage.

Now I tried to redo the code you gave me where I wanted to display this new info, however $db_total_hits keeps returning a value of 0.

Help me out again, please ...?

Also for the validation part, I haven't stuck it in yet, but I just realized I will really need it for the update section. Will putting it validate_record work for this, since modify_record checks there first...?
Quote Reply
Re: Two requests..... In reply to
 
Quote:
I stuck in the code for the $db_total_hits without a problem, and even added

if ($db_total_hits > "0" ){
print qq| "Welcome, $db_userid. You have $db_total_hits validated records etc | ;
}

Good idea! I never think of things like that until I try it and get a result that looks ugly. :-)

Quote:
I would also like to show how many(total) records in the database are validated, how
many have a field value of "Yes" for {'Popular'}, etc. As I add more fields I would like to be able to add more info of this type to the homepage.

Will the contents of all the fields you want to count be either "Yes" or "No" (or "Yes" or empty)? I really need to know that before I continue because it makes a difference in the structure of the code. (BTW, it makes it a whole lot easier if they are that way! Smile )



------------------
JPD





Quote Reply
Re: Two requests..... In reply to
Yes, the field contents will be either "Yes" or "No" or something like that but definitely not empty ....
Quote Reply
Re: Two requests..... In reply to
Code:
open (DB, "<$db_file_name") or &cgierr("unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
foreach $line (@lines) {
@array = &split_decode($line);

At this point, you have the entire database in an array called @lines -- $lines[0] is the first record, $lines[1] is the second record, etc. The "foreach" loop (which is started above) takes each line and turns the record into an array, so that the contents of the first field are in $array[0], etc.

The next part is where you actually look for the value of the field. Presumably you want to look only at validated records, so this would follow right after the previous code

Code:
if ($array[# eq "Yes") { # replace # with the field number of your Validated field
++$num_valid; #counts total number of validated records

The next thing is to check for the values of the other fields. You'll need to use this same syntax for each field you want to count.

Code:
if ($array[#] eq "Yes") { # replace the # with the field number for Popular
++$num_popular;
}

Once you have all the fields listed that you want to count, close off the loops you started above:

Code:
}
}

So the whole thing looks like

Code:
open (DB, "<$db_file_name") or &cgierr("unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
foreach $line (@lines) {
@array = &split_decode($line);
if ($array[# eq "Yes") { # replace # with the field number of your Validated field
++$num_valid; #counts total number of validated records
if ($array[#] eq "Yes") { # replace the # with the field number for Popular
++$num_popular;
}
}
}

When you go to print it out, use the variables that were incremented above--

Number of validated records = $num_valid<BR>
Number of popular records = $num_popular

If you wanted to give a number of all records, validated or not, use

$total_num = scalar(@lines);

and in your print statement, use

Total number of records = $total_num

This is completely untested. I've looked over it for typos, but I may have missed some. (Maybe I should put this in my sig line! Smile )

------------------
JPD





Quote Reply
Re: Two requests..... In reply to
Thanks JPD,

I lost net access for a while, but now that I'm back I'll give it a whirl. Thanks for the quick reply !!

web dog