homepage Welcome to WebmasterWorld Guest from 54.161.192.130
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Need script help
Creating a 'select' widget
randallmarquis



 
Msg#: 4168713 posted 6:42 pm on Jul 12, 2010 (gmt 0)

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


<table width=450 CELLPADDING=0 CELLSPACING=0 BORDER=0 BGCOLOR="#FFFFff">
<tr><td><font face=Arial size=2>Pick which Company you wish to modify:
<P>
<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>
</FORM>
$html_menu
$html_footer
</font>
</td></tr></table>


The select box 'output' looks like this:
<P>
<FORM ACTION="http://lender-reviews.com/cgi/dbmod.cgi/lender/lynn90967.htm" METHOD="GET">
<P><center><SELECT NAME="modify"><OPTION>---<OPTION>1ST ALLIANCE LENDING, LLC<OPTION>1ST COMMONWEALTH BANK OF VIRGINIA<OPTION>1ST MARYLAND MORTGAGE CORPORATION<OPTION>A PLUS MORTGAGE SERVICES INC<OPTION>ACCESS NATIONAL MORTGAGE CORPORATION<OPTION>ACHIEVA CREDIT UNION<OPTION>ACOPIA LLC</SELECT><P><INPUT TYPE="HIDDEN" NAME="modify_form_record" VALUE="1"> <br>
<center><INPUT TYPE="SUBMIT" VALUE="Modify"> <INPUT TYPE="RESET" VALUE="Reset Form"></center><br>
</FORM>


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;
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: $!");
@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:

5111|ACOPIA LLC|BRANT PHILLIPS|306 NORTHCREEK BLVD STE

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

 

phranque

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



 
Msg#: 4168713 posted 2:01 am on Jul 13, 2010 (gmt 0)

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].

randallmarquis



 
Msg#: 4168713 posted 2:53 am on Jul 13, 2010 (gmt 0)

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.'

phranque

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



 
Msg#: 4168713 posted 4:02 am on Jul 13, 2010 (gmt 0)

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";

randallmarquis



 
Msg#: 4168713 posted 4:18 am on Jul 13, 2010 (gmt 0)

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
randallmarquis@gmail.com

solly

5+ Year Member



 
Msg#: 4168713 posted 5:59 am on Jul 13, 2010 (gmt 0)

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 ]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved