Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Walk-through how to Add Select Menu?

(Page 1 of 2)
> >
Quote Reply
Walk-through how to Add Select Menu?
hi - i've searched but the forum but there doesn't seem to be a clear step-by-step answer to this - maybe I missed it...

can anyone provide step-by-step instructions on how to add a select menu to LinkSQL Links table and have it display in the users Add and Modify Link pages.

for example sakes let's say the menu name is "DAYS" and the selections are: One, Two, Three

I have added a select field to the database and it's displaying fine in the admin area, but it's a no-show on the user add page when I enter the field <%DAYS%>.

Thanks
regan

Last edited by:

ryel01: Mar 13, 2004, 8:40 PM
Quote Reply
Re: [ryel01] Walk-through how to Add Select Menu? In reply to
Create a new global called "DAYS" with the following value:

Code:
sub {
my $tags = shift;
my $table = $DB->table("Links");
my $html = $DB->html($table, $IN);
my $form = $html->select({name => "DAYS"});
return $form;
}

Documentation in the help pages seems to be missing for this, but there's several options available for default values, name/value pairs etc. Have a look at sub select() in GT::SQL::Display::HTML.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Walk-through how to Add Select Menu? In reply to
Fantastic - thanks Philip!!!!!

The same global could be used for any new menu added to the links table couldn't it? ie if the global was called something like 'build_link_select_menu" and was called with a Column name passed to it?

What I mean is something like...

<%build_link_select_menu('DAYS');%>

Do you know how I could modify this global to work that way?

cheers!

regan
Quote Reply
Re: [fuzzy logic] Walk-through how to Add Select Menu? In reply to
i think i might have cracked it...

EDIT>> NOPE - the code below forgets the user selection if the form is returned with an error, because I've taken the "Tags" line out. How would I go about keeping the Tags but also passing in the other value?

Tag to go into the template...

<%build_select_menu(Days)%>

global to add called "build_select_menu"...

sub {
my $column = shift ;
my $table = $DB->table("Links");
my $html = $DB->html($table, $IN);
my $form = $html->select({name => "$column"});
return $form;
}

regan

Last edited by:

ryel01: Mar 13, 2004, 10:30 PM
Quote Reply
Re: [ryel01] Walk-through how to Add Select Menu? In reply to
As far as I know, the tags hashref is the only argument passed to globals, so you can't set up a global to do what you want. Instead, you'll have to create a module containing methods that you can than call in your templates.

Create a new file in your admin directory called "MySubs.pm", and add the following code to it:

Code:
package MySubs;

use strict;
use GT::SQL;
use GT::CGI;

use vars qw/$DB $IN/;

$DB = new GT::SQL ("/path/to/cgi-bin/links/admin/defs"); #change this
$IN = new GT::CGI;

sub select {
my $db = shift;
my $column = shift;
my $default = shift;

my $table = $DB->table($db);
my $html = $DB->html($table, $IN);
my $form = $html->select({name => $column, value => $default});

return $form;
}

1;

MySubs::select() requires 2 arguments: table name, and column. An optional third argument, the default selected value, may also be passed.

In your template, you can now use:
Code:
<%MySubs::select("Links", "Days")%>

This works fine for me, but PLEASE note that this is all new to me, so I'm sure there may be other/better ways to do this.

Philip
------------------
Limecat is not pleased.

Last edited by:

fuzzy logic: Mar 13, 2004, 11:15 PM
Quote Reply
Re: [ryel01] Walk-through how to Add Select Menu? In reply to
Just put

my $tags = shift;

after

my $column = shift;
Quote Reply
Re: [fuzzy logic] Walk-through how to Add Select Menu? In reply to
Code:
As far as I know, the tags hashref is the only argument passed to globals, so you can't set up a global to do what you want.


Used to be the case I think - but you definitely can now.
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
In Reply To:
Just put

my $tags = shift;

after

my $column = shift;


hmmmm... nope, that didn't seem to work. if you look at the bottom of the snippet below you'll see the variable that's being returned to the page - still doesn't remember the selection.

Code:
sub {

my $column = shift;
my $tags = shift;
my $table = $DB->table("Links");
my $html = $DB->html($table, $IN);
my $form = $html->select({name => "$column"});
return $form;
}


$VAR = 'CODE(0x8494d74)';
Quote Reply
Re: [ryel01] Walk-through how to Add Select Menu? In reply to
Try this - you need to call it with <%globalname('ColumnName',$ColumnName)%>

sub {
my $column = shift;
my $selected = shift;
my $table = $DB->table("Links");
my %hash;
my $i = 0;
my $names = $table->form_names->{$column};
my $vals = $table->form_values->{$column};
for my $val (@$vals) {
$hash{$val} = $names->[$i++];
}
my $html = $DB->html($table, $IN);
my $form = $html->select({
name => "$column",
values => \%hash,
value => $selected });
return $form;
}

Last edited by:

afinlr: Mar 14, 2004, 3:08 PM
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
nope, still doesn't seem to remember the selected value. Unimpressed
Quote Reply
Re: [ryel01] Walk-through how to Add Select Menu? In reply to
And you have <%build_select_menu('Days',$Days)%> in your template?
I just tested this on my site and it appears to be working.
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
aaggggghhhhhh - I forgot to remove another of the globals I was testing with.... YES IT WORKS!!! WOO HOO!!

Thanks so much - you're on a roll today!

This is one question that has been hanging around forever - should add it to the resources section so it's easy to find.

Cheers!

Regan
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
and in case anyone was wondering - you can sort the list alphabetically too...

Code:
sub {
my $column = shift;
my $selected = shift;
my $table = $DB->table("Links");
my %hash;
my $i = 0;
my $names = $table->form_names->{$column};
my $vals = sort $table->form_values->{$column};
for my $val (@$vals) {
$hash{$val} = $names->[$i++];
}
my $html = $DB->html($table, $IN);
my $form = $html->select({
name => "$column",
values => \%hash,
value => $selected });
return $form;
}

r
Quote Reply
Re: [fuzzy logic] Walk-through how to Add Select Menu? In reply to
>> As far as I know, the tags hashref is the only argument passed to globals, so you can't set up a global to do what you want.
>> Instead, you'll have to create a module containing methods that you can than call in your templates.

Not quite true.

There have been changes to the variables between 1.x and 2.x versions, but you have access to pretty much everything from within the globals file.

You don't need to 'use' any of the vars. You have automatic access to:

$CFG, $USER, $DB as well as the passed in tags hash.

your environment is set up, as if you were running in a plugins .pm. If you need a plugins' variables, you must request them, same as you would anywhere else.

You *do* need to init the 'date' variables if you want to use the date routines.

This is a bit simplified, but as I've said in other messages, the template parser is *AWESOME* and you have power probably equal to PHP from inside it.

While you can write large globals, it imposes some overhead in loading the file, especially if you don't need the file for every template. It also makes it harder to maintain the globals in that format. If you have a complicated global, or one that is called only infrequently or from one template, you'd be better off creating a separate module and placing it in the Plugins directory, then calling it with <%Plugins::Modulename::functionname( params )%> from inside the template. It will be loaded and parsed only when needed, rather than with every template.

This also works if you need to group a few related functions, like my Days_Old.pm. They are all in one file, and can be accessed easily. They are not scattered through the globals.txt file, and are kept in a nice, neat perl format, which makes maintaining and updating the code easier.

Just a few thoughts.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Walk-through how to Add Select Menu? In reply to
Sweet Sly

As I've said before, I'm very much still in the learning phase here. Do I understand correctly that the tags hashref isn't passed to globals when they're called as methods?

Philip
------------------
Limecat is not pleased.

Last edited by:

fuzzy logic: Mar 14, 2004, 9:13 PM
Quote Reply
Re: [fuzzy logic] Walk-through how to Add Select Menu? In reply to
ok, i've tried every iteration of the above for about 4 hours now. Pulling hair out. can someone cover the steps 1, 2, 3 for me? Seems like things jump around and i'm not sure what from above is necessary? I created the MySubs.pm and included that <$MySubs::> etc. as well as build_menu_select and DAYS globals. The final global doesn't look like it needs the MySubs.pm yes?

I tried with just the build_menu_select with column names called and I get empty select menus. I'm trying to add drop downs for dates (day, month, year) and states, and preserve them in the modify page...

help, please Unsure
Quote Reply
Re: [pshadow] Walk-through how to Add Select Menu? In reply to
Hi,

In Reply To:
Pulling hair out.[/quote]
I know that feeling Wink.

All you should need is the global above. Call it build_select_menu.
Then in your template you need

<%build_select_menu('Days',$Days)%>

where the first option is the name of your field - e.g. 'States' - and the second option is the name of your field with a $ in front - e.g. $States
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
thanks for your help, but still can't get it working...

I created new global in admin called build_select_menu with the following:

sub {
my $column = shift;
my $selected = shift;
my $table = $DB->table("Links");
my %hash;
my $i = 0;
my $names = $table->form_names->{$column};
my $vals = sort $table->form_values->{$column};
for my $val (@$vals) {
$hash{$val} = $names->[$i++];
}
my $html = $DB->html($table, $IN);
my $form = $html->select({
name => "$column",
values => \%hash,
value => $selected });
return $form;
}

then pasted <%build_select_menu('State',$State)%> into my template and all I get is an empty slect menu. :(

do I need to create a new table with the default state names? currently it's just a drop down option in the add template so the Links table has a State column populated with state abbr. (AL, CA, etc.) for listings from those states. (not all states are currently represented in the State column as not every state category has a link entry.

any ideas?
Quote Reply
Re: [pshadow] Walk-through how to Add Select Menu? In reply to
The State field must already contain all the possible options with the relevant one selected (or not). From what you've said, I don't think you have this? (Otherwise how will it know what the options are - the global isn't quite that clever Wink)
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
i can make sure to add an entry for each state to insure they are all represented. however, with the code I've used I still can't get it to show any of the State fields entires?
Quote Reply
Re: [pshadow] Walk-through how to Add Select Menu? In reply to
Can you describe your State column a bit more. If you go to admin->database->Links->properties and click on State - you must have all the states in the form_names and form_values boxes as this is where the global gets them from. I don't think you necessarily have to define the column as a select column - it could be radio or checkbox and I think the global should still work - but it should be one of these.
Quote Reply
Re: [afinlr] Walk-through how to Add Select Menu? In reply to
you are the man, you are the man, you are the man... that did it. I never thought to check there. I was so certain I had populated those fields during the initial set-up. with all the updates and re-syncs I must have lost the data in there. re-populated and works like a charm (so far) fingers crossed.

thanks again for your time and patience. so grateful. now if i can just replace all the hair i've lost of the last 2 days things would be great. :)
Quote Reply
Re: [pshadow] Walk-through how to Add Select Menu? In reply to
A lovely global indeed!

Two questions;
1) Is there anyway to add a CSS class field into the created select form?
i.e. I want to add class="form_data" so I can format the drop down box.
I've tried playing with the following line, but don't know how to add to it?;
name => "$column",

2) Is there anyway to change the default value for a select box from "---" to something like
"Please select a Country?" - thus making the user more likely to click on the select box.

Any help is appreciated,
Piers
Quote Reply
Re: [Piers1] Walk-through how to Add Select Menu? In reply to
Hi,

No problem =)

Regarding how to automate the SELECT dropdowns, you could try;

FormatField ===>

Code:
sub {

my $column = $_[0];
my $selected = $_[1];
my $table = $DB->table('Users');
my %hash;
my $i = 0;
my $names = $table->form_names->{$column};
my $vals = sort $table->form_values->{$column};
for my $val (@$vals) {
$hash{$val} = $names->[$i++];
}

my $html = $DB->html($table, $IN);
my $form = $html->select({
name => "$column",
values => \%hash,
value => $selected });
return $form;
}

Then call with;

<%FormatField('FieldName',$FieldName)%>

That should then take into account any selected values that exist (and also lets it work on modify.cgi =)).

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Walk-through how to Add Select Menu? In reply to
As per couple of post above....

2) Is there anyway to change the default value for a select box from "---" to something like
"Please select a Country?" - thus making the user more likely to click on the select box.

Could you explain how to get the "Please select a Country?" select to work.

Thanks

Regards

minesite
> >