Gossamer Forum
Home : Products : DBMan : Customization :

Build_Select _Field_From_DB

Quote Reply
Build_Select _Field_From_DB
I noticed the following section of code while perusing db.cgi "sub build_select_field_from_db {"

How does one utilise this section of code - I don't see any reference thereto in the text file.

Thanks in advance
Quote Reply
Re: Build_Select _Field_From_DB In reply to
I'm not sure that it's documented at all.

Use it just as you would use build_select_field--

print &build_select_field_from_db("FieldName","$rec{'FieldName'}");

It will go through your database and find all the unique values in the field, and then create a select field from the values.


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





Quote Reply
Re: Build_Select _Field_From_DB In reply to
I would like to be able to select multiple categories within a select field. For instance, at the Community College where I work, we have employees who teach classes in multiple subject areas. I have created a select field known as "AcadArea". As of right now, I can only select one category within this field.

Is there a way to be able to select multiple categories within one select field? (Just like selecting "Related Categories" in LINKS.)

Any help would be greatly appreciated. (BTW: I did search through this Forum for an answer. But the only closest and relevant answer dealt with modifying the "search option".)

Regards,

Eliot Lee

[This message has been edited by Eliot (edited May 13, 1999).]
Quote Reply
Re: Build_Select _Field_From_DB In reply to
Yes, you can use a multiple select field for inputting data. Where you run into problems is having a multiple select field in a search form.

This is what I did. I set up one form for searching (html_search_form) and one for adding and modifying (html_record_form). Within the html_record_form definition, I used

Code:
<TR><TD align=right valign=top><$font>Category:</font></td>
<TD>|;
print &build_mult_select_field("Category",$rec{'Category'},3);
print qq|<BR><$font>Hold the <B>Ctrl</B> key to select more than one.</font>
<BR><input type=text name="Category" size="20" maxlength="255">  
<$font>Add a new category</font></td></tr>

You'll notice there is a select field and a text field with the same name. That's so I can add a new category on the fly without having to change my .cfg file. I can even add more than one category, if I put a | between them in the text field.

Okay. Now for the subroutine. I made a new one so I wouldn't lose my old build_select_field_from_db subroutine.

The following goes into db.cgi, around where all the "build" subroutines are.

Code:
sub build_mult_select_field {
# --------------------------------------------------------
# Builds a SELECT field based on information found
# in the database. Parameters are the column to build,
# a default value, a default name,
# and the size of the box.

my ($column, $value, $size, $name) = @_;
my (@fields, @values, $output);

$name | | ($name = $column);
$size | | ($size = 1);

@values = split (/\Q$db_delim\E/,$value);

for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}

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);
my (@tempfield) = split (/\Q$db_delim\E/o, $fields[$fieldnum]);
foreach $field (@tempfield){
if (!(grep $_ eq $field, @selectfields)) {
push (@selectfields, $field);
}
}
}
close DB;

$output = qq|<SELECT NAME="$name" MULTIPLE SIZE=$size><OPTION>---|;
foreach $field (sort @selectfields) {
($value =~ $field) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";

return $output;
}

When you use the subroutine, use
print &build_mult_select_field("FieldName",$rec{'FieldName'},3);

The 3 is the number of rows your select field will show.

There's one other thing you need to do.

In db.cgi, sub parse_form

after

if ($value eq "---") { next PAIR; }

add

unless ($value) { next PAIR; }

As I said, though, you will run into trouble if you use this in a search form. I'd use build_select_field_from_db on the form that searches.

You will need to change that subroutine, too, though, to break up your multiple selections.

Code:
sub build_select_field_from_db {
# --------------------------------------------------------
# Builds a SELECT field from the database.

my ($column, $value, $name) = @_;
my (@fields, $field, @selectfields, @lines, $line, $ouptut);
my ($fieldnum, $found, $i) = 0;

$name | | ($name = $column);

for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
last;
}
}
if (!$found) {
return "error building select field: no fields specified!";
}

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);
@options = split (/\Q$db_delim\E/o, $fields[$fieldnum]);
foreach $option (@options) {
if (!(grep $_ eq $option, @selectfields)) {
push (@selectfields, $option);
}
}
}
close DB;

$output = qq|<SELECT NAME="$name"><OPTION>---|;
foreach $field (sort @selectfields) {
($field eq $value) ?
($output .= "<OPTION SELECTED>$field") :
($output .= "<OPTION>$field");
}
$output .= "</SELECT>";

return $output;
}

You probably can put the two subroutines together. But this works for me.

IMPORTANT!! Don't forget to take out the spaces between the &#0124; &#0124; characters!!!!!
------------------
JPD


[This message has been edited by JPDeni (edited May 13, 1999).]
Quote Reply
Re: Build_Select _Field_From_DB In reply to
I know it has been a few months since I first raised the question about multiple select fields...not based on the getting values from fields, but creating a multiple select field textarea menu.

I do not know if this has been answered yet. But ideally, I'd like to have registered users be able to select from multiple pre-made values (like the db_select_fields) and if possible to allow users to select multiple values from the search form.

Any ideas?

I tried using the codes you provided, but it did not do what I wanted. I also tried modifying the sub build_select_fields routine in db.cgi and was unsuccessful.

Thanks in advance.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Build_Select _Field_From_DB In reply to
Building a select field that allows users to select more than one option when adding or modifying isn't a problem. The build_select_field subroutine in Links has that capability.

The problem comes in when you're doing a search with a multiple select field.

If, for example, you add a record and select

Option1
Option2
Option3

I come along and do a search and select

Option1
Option3

Your record will not be returned in the search.


PS Sorry about your car troubles. What a drag!



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





Quote Reply
Re: Build_Select _Field_From_DB In reply to
Thanks, Carol. Smile

I will check out the LINKS routines tomorrow if I am still in town...taking my car into the shop tomorrow morning...*cross fingers* Hopefully, they can get it back to me in a day or so...(So, I can salvage a part of my week off.)

Smile

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us