Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Need script help

Creating a 'select' widget



6:42 pm on Jul 12, 2010 (gmt 0)

5+ Year Member

Here is the code from a page that creates a select box list from records in a database:

<tr><td><font face=Arial size=2>Pick which Company you wish to modify:
<FORM ACTION="$db_script_url" METHOD="GET">
<P><center>|; print &build_select_field_from_db ("name", "", "modify"); print qq|</center>
<P><INPUT TYPE="HIDDEN" NAME="modify_form_record" VALUE="1">
<center><INPUT TYPE="SUBMIT" VALUE="Modify"> <INPUT TYPE="RESET" VALUE="Reset Form"></center>

The select box 'output' looks like this:
<FORM ACTION="http://lender-reviews.com/cgi/dbmod.cgi/lender/lynn90967.htm" METHOD="GET">
<center><INPUT TYPE="SUBMIT" VALUE="Modify"> <INPUT TYPE="RESET" VALUE="Reset Form"></center><br>

The routine that creates the content is "build_select_field_from_db"

Here is the routine in full:

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;
for ($i = 0; $i <= $#db_cols; $i++) {
if ($column eq $db_cols[$i]) {
$fieldnum = $i; $found = 1;
if (!$found) {
return "error building select field: no fields specified!";
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
@lines = <DB>;
close DB;
LINE: foreach $line (@lines) {
if ($line =~ /^#/) { next LINE; } # Skip comment lines.
if ($line =~ /^\s*$/) { next LINE; } # Skip Blank Lines.
chomp ($line);

@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$fieldnum], @selectfields)) {
push (@selectfields, $fields[$fieldnum]);
if ($name) { $output = qq|<SELECT NAME="$name"|; }
else { $output = qq|<SELECT NAME="$column"|; }
$output .= "><OPTION>---";
foreach $field (sort @selectfields) {
if ($field eq $value) {
$output .= "<OPTION SELECTED>$field";
else {
$output .= "<OPTION>$field";
$output .= "</SELECT>";
return $output;

And therein lies my problem. I need the select box content changed to include the value of the database key, which is the first column in the database (known as 'record-number') also known as column '0' (zero).

This line: $output .= "<OPTION>$field"; Needs to be changed to this somehow: $output .= "<OPTION value=\"record-number\">$field";

Where the above 'record-number' actually pulls the data from that record.

Here's what a line out of the database looks like:


The actual desired outcome is to create a 'SELECT' tag that looks like this:

<OPTION value="5111">ACOPIA LLC

Simple? I don't know... I tried $db_key but it only returned the name of the field (record-number) and not the data. I tried $record-number and that only returned '-number' but not the data.

Stumped... and standing by all day today for ideas.

Randall Marquis


2:01 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

welcome to WebmasterWorld [webmasterworld.com], Randall!

i would put all the record numbers and option text in a perl hash [perldoc.perl.org].
then sort the hash by value [perldoc.perl.org] or by keys [perldoc.perl.org].


2:53 am on Jul 13, 2010 (gmt 0)

5+ Year Member

I'm afraid I don't know enough to be able to use that information...

I'm hoping that someone knowledgable will look at the code, and see a simple fix that says " Oh! Just sub this for that!"

I'm not that good at this stuff... and wish I was.

WHY can't the line: $output .= "<OPTION>$field";

...be re-written as: $output .= "<OPTION value=record-number>$field";

...where 'record-number' reads the field that is the $db-key (1st column) ?

After all... it reads in the 2nd column at '$field,' otherwise known as 'name.'


4:02 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

it won't work that way because you haven't associated the 1st column with the 2nd column in your data structure.
all you have done is create an array of 2nd column values from which you got $field in your foreach loop.
if you want to do it the hard way, you can find in your DB file the record number of each 2nd column value but that only works if your 2nd column values are unique in that file.
if you want to do it the easy way, you will learn how to use hashes and references, which will pay immense benefits in your future exploitation of perl's capabilities.
we will gladly show you where to learn about those capabilities and guide you through your attempts to implement them.
what you won't get here is a "Fix My Code/Do My Homework" [webmasterworld.com] answer.

here's a hint to get you started.
instead of this:
push (@selectfields, $fields[$fieldnum]);
you need to find the "fieldnum" of both fields and do something like this:
$selectfieldshashref->{$fields[$namefieldnum]} = $fields[$recordnumberfieldnum];

then sort on the hash keys.
then do a for loop on the sorted hash keys and use an assignment like this:
$output .= "<OPTION value=\"$selectfieldshashref->{$key}\">$key";


4:18 am on Jul 13, 2010 (gmt 0)

5+ Year Member

okay. I think I get it... kinda'.

But the learning curve is too time consuming.

I hate to do it, but the quick solution is to just ditch the 1st column all together and let the company name be the 'key' which may cause confusion if two companies have the same name.

I'm not equipped or funded enough at this point to get into code re-writes, or learning to change this stuff.

Thanks for the help though. This script is about to put the largest database of mortgage lenders in the US on the internet in a searchable format... and I maybe have a few days to fix this... not a week studying books and code trials.

I'll just go do the best I can with what I got.

Randall Marquis


5:59 am on Jul 13, 2010 (gmt 0)

10+ Year Member

Hi, Randall. I hope you don't run into too much trouble with your fix (phranque made some good suggestions). I would also strongly recommend you use the use strict; pragma to catch the typos. Just put "use strict;" at the top of your script.

[perl.com ]

Featured Threads

Hot Threads This Week

Hot Threads This Month