PDA

View Full Version : Need help with a for loop


ynotlim
09-02-2006, 02:03 AM
I am creating a drop down selection box. Here is how it is coded right now

if ($major eq"") {print "<option value='' selected>Select";}
else {print "<option value=''>Select";}
if ($major eq"77279") {print "<option value='77279' selected>Aerospace Engineering";}
else {print "<option value=77279''>Aerospace Engineering";}
if ($major eq"60014") {print "<option value='60014' selected>African American Studies";}
else {print "<option value='60014'>African American Studies";}
if ($major eq"65063") {print "<option value='65063' selected>Anthropology";}
else {print "<option value='65063'>Anthropology";}
if ($major eq"97068") {print "<option value='97068' selected>Applied Ecology";}
else {print "<option value='97068'>Applied Ecology";}
if ($major eq"60093") {print "<option value'60093' selected>Art History";}
else {print "<option value='60093'>Art History";}
if ($major eq"5744D") {print "<option value'5744D' selected>Arts and Humanities";}
else {print "<option value='5744D'>Arts and Humanities";}
if ($major eq"57000") {print "<option value='57000' selected>Arts, Undeclared";}
else {print "<option value='57000'>Arts, Undeclared";}
if ($major eq"60100") {print "<option value='60100' selected>Asian American Studies";}
else {print "<option value='60100'>Asian American Studies";}

If a student already selected a Major, it pulls it from the database and previously selects it.

I have two database
1: for the Majors... table w/ Columns recno, major_code and major_name.
2: for the Students and one of the columns is student_major_code

here's what I think should be coded...

# for ($recno=1; $i<32; $i++) {
# $sel=' '; if ($major_code == $student_major_code) { $sel = 'SELECTED'; }
# print "<OPTION VALUE=\"$major_name{$major_code}\" $sel>$major_name\n";
# }

please help... give me the perl cgi script for connecting to both databases and all the other stuff

4um
09-02-2006, 02:41 AM
ynotlim, I don't know anything about your databases, their format or whatever, so I'll pass on "connection" request.

Assuming the content of the databases will be put into a hash, something like this might be done:

my %List = (
'77279' => 'Aerospace Engineering',
'60014' => 'African American Studies',
'65063' => 'Anthropology',
'97068' => 'Applied Ecology',
'60093' => 'Art History',
'5744D' => 'Arts and Humanities',
'57000' => 'Arts, Undeclared',
'60100' => 'Asian American Studies',
);

my $major = '79068';

print "<option value=''>Select</option>";
for(keys %List)
{
print "<option value='$_' ";
print 'selected' if $major eq $_;
print ">$List{$_}</option>";
}

Note that the first in the dropdown list will be selected by default if none of the others are selected.

NiteOwl
09-02-2006, 07:55 AM
Hope it helps.


my %List = (
'77279' => 'Aerospace Engineering',
'60014' => 'African American Studies',
'65063' => 'Anthropology',
'97068' => 'Applied Ecology',
'60093' => 'Art History',
'5744D' => 'Arts and Humanities',
'57000' => 'Arts, Undeclared',
'60100' => 'Asian American Studies',
);

print "\n";

my $major = '60093';
print "<form action=\"http\:\/\/yourcgiscript\" method=\"post\">\n<select name=\"variable name\">\n";
print "<option value=''>Select</option>\n";
for(keys %List)
{
print "<option value='$_' ";
print 'selected' if $major eq $_;
print ">$List{$_}</option>\n";
}

print "</select></form>";

ynotlim
09-02-2006, 08:06 AM
thanks for your help guys. I really appreciate it. I'll give it a try when I go back to work on Tuesday. Have a good labor day weekend. :thumbsup:

FishMonger
09-02-2006, 08:41 AM
NiteOwl,

It's best not to use the full url in the action attribute unless the script is outside of your domain. Using the full url forces the web server to do a dns query for each submission. It's much more efficient to use relative or absolute paths.

Also, the use of "leaning tower" escapes is messy (there are better methods of quoting) and escaping the / in the url is unnecessary.

FishMonger
09-02-2006, 08:43 AM
ynotlim,

If you provide us with more info about your databases, we can give you examples on the best methods to access them.

NiteOwl
09-02-2006, 09:09 AM
print <<HTMLF;
# some code
HTMLF

or
print qq`
#some code
`;

would have be better choices.

:D

FishMonger
09-02-2006, 09:28 AM
Here's a better approach.
use CGI;

my $cgi = new CGI;

# for this example I'll set the default to 97068
my $major = $cgi->param('major') || '97068';

my %courses = (
'77279' => 'Aerospace Engineering',
'60014' => 'African American Studies',
'65063' => 'Anthropology',
'97068' => 'Applied Ecology',
'60093' => 'Art History',
'5744D' => 'Arts and Humanities',
'57000' => 'Arts, Undeclared',
'60100' => 'Asian American Studies',
);
my @codes = keys %courses;

print $cgi->header, $cgi->start_html,
$cgi->p('Select your major: '),
$cgi->start_form,
$cgi->popup_menu(-name=>'major',
-values=>\@codes,
-default=>$major,
-labels=>\%courses,
),
$cgi->submit('Submit'),
$cgi->end_form,
$cgi->end_html;
http://search.cpan.org/~lds/CGI.pm-3.23/CGI.pm

ynotlim
09-02-2006, 09:58 PM
I have two databases. One stores allll the majors and major codes and the other one stores all the students information. Hope that helps.

if the student is updating his information. the major he previously picked needs to be already selected. So it needs to check where stu_major_code = major_code and add the selected statement.

thank you for the help guys

database#1: majors
Table: majors
columns:
recno
major_code
major_name

database#2: student_info
table: students
columns:
name...etc
stu_major_code

FishMonger
09-02-2006, 10:18 PM
Your database info is a little too vauge.

On your student_info database, what does "name...etc" mean?

What type of database, mysql, csv text file, or ??

How do you want to utilize the databases?

What code are you currently using to access the databases?

Maybe reading over the documentation of the DBI module might help.
http://search.cpan.org/~timb/DBI-1.52/DBI.pm

You may also want to read up on database Normalization.
http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

ynotlim
09-04-2006, 06:40 PM
1. Name...etc means that there are more columns on that database, but that doesn't matter b/c you only need stu_major_code. $major will equal stu_major_code.

2. I am using mysql

3. I want to utilzed the database just like this:
my %List = (
'77279' => 'Aerospace Engineering',
'60014' => 'African American Studies',
'65063' => 'Anthropology',
'97068' => 'Applied Ecology',
'60093' => 'Art History',
'5744D' => 'Arts and Humanities',
'57000' => 'Arts, Undeclared',
'60100' => 'Asian American Studies',
);
I want to put my database into a hash like this. So how do i store my database information into my %list??

thanks in advance
Tony

FishMonger
09-04-2006, 07:02 PM
There are several ways to build the hash, here's one.
my %List;

my $sth = $dbh->prepare("SELECT major_code, major_name FROM Majors");
$sth->execute;

while(my ($class_code, $major) = fetchrow_array) {
$List{$class_code} = $major;
}
You can also use fetchall_hashref.

The documentation for the DBI module has examples of both of these methods as well as a couple others.