Gossamer Forum
Home : Products : DBMan : Customization :

Listing each value of a field.

Quote Reply
Listing each value of a field.
Let's say I have a standard input text field (not a select field), called "Sections". Is there a way to list everything in that field in the following format:

General
testing
Upper Management

It seems simple, but I don't want it to repeat if there are multiple instances like:

General
General
testing
Upper Management
Upper Management
Upper Management

This would be a big help to me. Thanks!
Quote Reply
Re: Listing each value of a field. In reply to
Yep. We'll just steal a little bit of code from &build_select_field_from_db:

Code:
$fieldnum = the number of the field you want to search
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$fieldnum], @values)) {
push (@values, $fields[$fieldnum]);
}
}
close DB;

When you want to print it out, use

Code:
foreach $field (sort @values) {
priint "$field<BR>";
}

You should get an alphabetical list of all the values in that field, but each will only be listed once.


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





Quote Reply
Re: Listing each value of a field. In reply to
Oh my goodness! Worked like a charm. Thanks again JPDeni for saving the day.

I've been trying to throw an if statement in there. What am I doing wrong?

foreach $field (sort @values) {
if ($rec{'Category'} eq "Planning Guides") { print "$field<BR>"; }
}
Quote Reply
Re: Listing each value of a field. In reply to
For one thing, I would probably put the "if" statement on the outside:

Code:
if ($rec{'Category'} eq "Planning Guides") {
foreach $field (sort @values) {
print "$field<BR>";
}
}

That seems to be what you want to do. That way the script only has to evaluate the "if" statement once.

I'm not sure about any other problem. I take it you have added the code to sub html_record and that you do have a field called "Category" and that one of the possible options is "Planning Guides."

I might need a little more context for the code in order to give you any more ideas.


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





Quote Reply
Re: Listing each value of a field. In reply to
Thanks JPDeni. This code is placed in a blank page, very similar to html_home.

It seems to be ignoring the $rec. I tried $in and I also tried putting my (%rec) = @_; at the top of the page, but to no avail.

The code you gave works great, it lists everything like it should. What I need now is to list stuff that matches some addition criteria. In this case: if ($rec{'Category'} eq "Planning Guides")

This would reduce the listing from everything, down to the records that matched the extra criteria.
Quote Reply
Re: Listing each value of a field. In reply to
In order to have something in the %rec hash, you first must do a search.

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





Quote Reply
Re: Listing each value of a field. In reply to
Doh!
Quote Reply
Re: Listing each value of a field. In reply to
 Smile

If you wanted to call the page subroutine with a link, you could add a field value into the link and then use

if ($in{'Category'} eq "Planning Guides") {

which would mean that your link would have to include

&Category=Planning+Guides

Or you could include a link to a key value for a record

&$db_key=some+value

And then at the beginning of your page subroutine have

%rec = &get_record($in{$db_key});

If you did the second one, you could use

$rec{'Category'}

in your "if" statement.


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





Quote Reply
Re: Listing each value of a field. In reply to
Thanks so much for your help Carol. I tried your suggestions but they don't work.

Here is the subroutine:
Code:
sub html_view_cat1 {

$page_title = "$cat1";
&html_page_top;

print qq|
<p align="center">$cat1 Subcategories:</p>
<p align="center"> |;

$fieldnum = 2;
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$fieldnum], @values)) {
push (@values, $fields[$fieldnum]);
}
}
close DB;

foreach $field (sort @values) {
#if ($rec{'Category'} eq "Planning Systems") {
print qq| <A HREF="$db_script_link_url&view_records=1&ID=*&Category=Planning+Systems&SubCategory=$field&cs=1">$field</a><BR> |;
#}
}

print qq|</font></p>|;

&html_footer;
&html_page_bottom;
}

Notice the if statement is rem'ed out cuz it doesn't recognize the $rec, even with your suggestions.

I've been working on this for 2 days straight, and it's killing me because I'm so close Smile

P.S. Planning Guides has been changed to Planning Systems.



[This message has been edited by Katana Man (edited August 12, 1999).]
Quote Reply
Re: Listing each value of a field. In reply to
You're still not doing a search, which is why your $rec{'Category'} doesn't work.

Somewhere, there needs to be a place where you define what record you're looking at and you need to search for it. Then you assign the results to the %rec variable.

The link I gave you belongs in some other subroutine. That is the link that sends the person to your sub html_view_cat1.

Bottom line is, that you have to either send a value for the Category field with the link that takes you to view_cat1, or you have to send a value for the key field and then do a search in view_cat1.



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





Quote Reply
Re: Listing each value of a field. In reply to
Well, I tried another 2 days worth of trial and error with any possible combination of your above comments.

i.e.
...&Category=Planning+Systems&view_cat1=1
...&Category=Planning+Systems&view_records=1
...&$db_key=Planning+Systems&view_cat1=1
...&$db_key=ID&view_cat1=1 etc...


So with my plain-Jane html_view_cat1 page, I was wondering if we can go about this in a different manner.

In my default.cfg
Field#2=Category
Field#3=SubCategory

The following code is used to search field #2 (Category)
Code:
$fieldnum = 2;
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$fieldnum], @values)) {
push (@values, $fields[$fieldnum]);
}
}
close DB;

Is there a way to have it check field #3 in within this code? Here is a stab in the dark:
Code:
$fieldnum = 2;
$Subfieldnum = 3;
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);

if ($Subfieldnum eq "Planning Systems") {

@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$fieldnum], @values)) {
push (@values, $fields[$fieldnum]);
}
}
}
close DB;

I mean, I am so close with the original code. It does print out the values for field 2 perfectly. All I need now is to have it print those values IF field 3 equals "Planning Systems".
Quote Reply
Re: Listing each value of a field. In reply to
Oh. Now I see what you want to do. I completely misunderstood what the purpose of the code was.

You're close. Smile

Code:
$fieldnum = 2;
$Subfieldnum = 3;
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if ($fields[$Subfieldnum] eq "Planning Systems") {
if (!(grep $_ eq $fields[$fieldnum], @values)) {
push (@values, $fields[$fieldnum]);
}
}
}
close DB;

Do you understand what the code is doing? I'll explain if you want me to.


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





Quote Reply
Re: Listing each value of a field. In reply to
Thank you so much Carol! This code has created a 2 level category DBMan.

-->Main Categories
---->Sub Categories
------>Records

Last week you wouldn't accept any money for helping me with this, but I'm mailing you some anyways.

If anyone else has benefited from JPDeni's help, I encourage you to send her something for her hard work. I believe she also likes gift certificates from amazon.com.

I think I speak for everyone when I say we really appreciate your help! It would be a shame to lose you from this forum. Thank you Carol. We appreciate you very much.
Quote Reply
Re: Listing each value of a field. In reply to
James, you're a real sweetie. (Go get your wife to give you a big kiss on the cheek for me!! Smile )



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