Gossamer Forum
Home : General : Perl Programming :

Can't figure this out

Quote Reply
Can't figure this out
i have code to print out a select name form thing and print out the names of categories. I have a field in the category definition file called Edit. What i want is that if edit is selected 'Yes', leave the category in and if its no, take it out.

This it build_select_field from links.

Code:
sub build_select_field3 {
# --------------------------------------------------------
# Builds a SELECT field based on information found
# in the database definition.
#
my ($column, $value, $name, $mult) = @_;
my ($size, %values);

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

if (! exists $db_select_fields{$column}) {
$db_select_fields{$db_cols[$db_category]} = $db_select_fields{'Mult-Related'} = join (",", &emcategory_list);
}

if ($mult) {
@fields = split (/\,/, $db_select_fields{"Mult-$column"});
%values = map { $_ => 1 } split (/\Q$db_delim\E/, $value);
}
else {
@fields = split (/\,/, $db_select_fields{$column});

$values{$value}++;
}
($#fields >= 0) or return "error building select field: no select fields specified in config for field '$column'!";

$output = qq|<SELECT NAME="$name" $mult SIZE=$size><OPTION>|;
foreach $field (@fields) {
if ($field =~ m/\@$/) {
$field =~ s/.*(\w+)(?=\@$)\@$//g;
}

$values{$field} ?
($output .= "\n") :
($output .= "<OPTION>$field");
}

$output .= "</SELECT>";

return $output;
}

I have it now so that categories with @ at the end are taken out, but can't get ones with edit as No.

Quote Reply
Re: Can't figure this out In reply to
How about...

Code:
foreach (@fields) {
next if (/@$/);
next if (/No/i);
??

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Can't figure this out In reply to
no, i just got the same output which was all the categories in my db except for the ones that end with @
I don't know how it would work since db_utils.cgi will use links.def when i'm in the links.db on the admin. So my edit field will never get seen i think.
Quote Reply
Re: Can't figure this out In reply to
I was just about to reply to say that wouldn't work but you beat me to it :)

I personally would use:

Code:
sub build_my_select_field {

my $output = qq|<select name="Category">|;
open(DB, "<$db_category_name") || &cgierr("Couldn't open cat db : $!");
while (<DB>) {
chomp;
@data = &split_decode($_);
next if ($data[$db_category] =~ /@$/);
next if ($data[$db_edit] =~ /^No$/i);
$output .= qq|<option name="$data[$db_category]">$data[$db_category]|;
}
close(DB);
$output .= qq|</select>|;
return $output;
}
You'd need to make sure $db_category and $db_edit were defined.

Give it a try if you want. I know it isn't as flexible as the original sub but it should do what you want to build a category select list.

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Can't figure this out In reply to
ok that works great, thanks alot Paul.

Btw...
how would i go about sorting the select name?
All the cats were printed all over the place
I know i can't sort the array.

Quote Reply
Re: Can't figure this out In reply to
Try:

Code:
sub build_my_select_field {
my @output = ();
my $output = qq|<select name="Category">|;
open(DB, "<$db_category_name") || &cgierr("Couldn't open cat db : $!");
while (<DB>) {
chomp;
@data = &split_decode($_);
next if ($data[$db_category] =~ /@$/);
next if ($data[$db_edit] =~ /^No$/i);
push @output, $data[$db_category];
}
close(DB);
foreach (sort { $a cmp $b } @output) {
$output .= qq|<option name="$_">$_|;
}
$output .= qq|</select>|;
return $output;
}
Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Can't figure this out In reply to
you've saved me once again. exactly what i needed to get this done. thanks

Quote Reply
Re: Can't figure this out In reply to
Laugh


Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Can't figure this out In reply to
If you are concerned about formatting you can use:

Code:
$output .= qq|<option name="$_">$_\n|;
Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/