Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Dynamically generate a drop-down field for a field...

(Page 1 of 2)
> >
Quote Reply
Dynamically generate a drop-down field for a field...
Just thought I would post this... as it seems to work a treat for me =)

Code:
sub {

my $field = $_[0];

my $table = $DB->table('Links');
my $sth = $table->select( ["DISTINCT($field)"] );

my $back = qq|<select name="$field">|;

while (my ($name) = $sth->fetchrow_array) {
$back .= qq|<option>$name</option>|;
}

$back .= '</select>';
return $back;
}

Code:
<%global_name('FieldName')%>

For example,

Code:
<%select_GEN('Country')%>

.. which would then generate a select box called "Country".

Enjoy Smile

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] Dynamically generate a drop-down field for a field... In reply to
Andy,

Nice. This is something I've been struggling with for a long time.

2 Questions:

1) Does this generate the dropdown from the records in a table or does it generate it from the forum values you set in the admin panel?
2) Will this show the existing value (when you modify a record) as "selected" when you make changes?

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Dynamically generate a drop-down field for a field... In reply to
Quote:
Nice. This is something I've been struggling with for a long time.

Yeah, its something that has bugged me for a while too :(

Quote:
1) Does this generate the dropdown from the records in a table or does it generate it from the forum values you set in the admin panel?

It generated it based on a DISTINCT query on the Links table. I.e;

SELECT DISTINCT(LinkOwner) FROM lsql_Links;

... would give you a list of all the owners in your lsql_Links table, which have a listing (and thus can be used for a search.. i.e "search links from this user").

Just makes it a bit more helpful, as it doesn't show them search terms which won't yeild any results =)

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] Dynamically generate a drop-down field for a field... In reply to
nice mod
Quote Reply
Re: [Andy] Dynamically generate a drop-down field for a field... In reply to
hi, can this be modified to display only the field (eg. Country) for links in a certain category

<%global(fieldname,category)%>
Quote Reply
Re: [Andy] Dynamically generate a drop-down field for a field... In reply to
Hello Andy,

The Global one is very good.

But, it is possible to obtain the list alphabetically ?

A
B
C
D ...

Thank you for your assistance.

Mick
Quote Reply
Re: [mick31] Dynamically generate a drop-down field for a field... In reply to
Hi,

Sure thing. Just after;

Code:
my $table = $DB->table('Links');

..try adding;

Code:
$table->select_options('ORDER BY Title DESC');

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] Dynamically generate a drop-down field for a field... In reply to
Hi Andy:

I am trying to use this in my Add Form but am getting the following error:

Code:
Variable 'select_fields' is not a code reference

I am calling it as:
<%select_fields('Tracking')%>

Any ideas what's causing this?


Thanks,


Clint.
--------------------------
http://AffiliatesDirectory.com
The Affiliate Programs Directory
Quote Reply
Re: [Clint] Dynamically generate a drop-down field for a field... In reply to
Hi,

That normally means the global doesn't exist, or has extra charachters around the

Code:
sub {
...
}

...area.

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] Dynamically generate a drop-down field for a field... In reply to
Andy:

You're right! I added some comments before the 'sub'.

I also note that your global above pulls out 'all' values from the table, even though those that aren't listed in the Admin Panel. This global will only select those that have been entered into Admin and will also sort them. I found this elsewhere in the Forum - it is not my work...

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

To call:

<%global_name('ColumnName',$ColumnName)%>



Thanks,


Clint.
--------------------------
http://AffiliatesDirectory.com
The Affiliate Programs Directory

Last edited by:

Clint: Apr 24, 2005, 6:06 PM
Quote Reply
Re: [Clint] Dynamically generate a drop-down field for a field... In reply to
Hi,

does one of this work with the new LSQL3? I've tried but didn't have success ... I'm looking for a way to create an advanced search where the select-fields are created dynamically from the db ...

I have done it in former times with Links 2.0 like this:
http://www.algarve-ferienhaus.info/cgi-bin/links/suchen.cgi

any help is welcome ...

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de

Last edited by:

kailew: Dec 17, 2005, 2:40 AM
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi Kai:

The above does work with 3.04.


Regards,


Clint.
--------------------------
http://AffiliatesDirectory.com
The Affiliate Programs Directory
Quote Reply
Re: [Clint] Dynamically generate a drop-down field for a field... In reply to
Hi Clint,

I've tried this:

Code:
sub {
my $column = shift;
my $selected = shift;
my $table = $DB->table("Category");
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;
}

- saved the sub as "select_cat"

- and called it with

Code:
<%select_cat('Name',$ColumnName)%>

... but it returns only an empty select-box ...

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi Kai:

What columns within the 'Category' Table are you actually trying to pull data from?


Regards,


Clint.
--------------------------
http://AffiliatesDirectory.com
The Affiliate Programs Directory
Quote Reply
Re: [Clint] Dynamically generate a drop-down field for a field... In reply to
Hi Clint,

I like to pull Data from "Name" ...

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi,

If your field name is "Name", then you need to use;

<%select_cat('Name',$Name)%>

...not;

<%select_cat('Name',$ColumnName)%>

Wink

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] Dynamically generate a drop-down field for a field... In reply to
sorry, I noticed that mistake earlier and fixed it -> but no result.

I guess the reason could be, that I have to use a select-field? The name-field in category isn't a select-field ...

I'm looking for a way to pull out the data from the values of field and not from the select-options ...

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi,

What does your "properties" for that field look like? It won't work, unless the field is a SELECT. There are other globals for different types (i.e CHECKBOX, etc).

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] Dynamically generate a drop-down field for a field... In reply to
hi Andy,

the field is not SELECT ... it's a normal text field and I'm looking for a way to generate a drop-down also from "normal" fields.

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi,

Erm, if its not a SELECT field, it won't work =)

Can you show me some example data, of what your using?

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] Dynamically generate a drop-down field for a field... In reply to
Hi Andy,

it's just the NAME field of the category table. I didn't make any changes to it ...

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi,

Sorry, I really don't get what your trying to do Unsure

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] Dynamically generate a drop-down field for a field... In reply to
Hi Andy,

I like to pull out all values of the NAME field (and other fields) and generate a drop-down which I like to use for a advanced search.

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
Quote Reply
Re: [kailew] Dynamically generate a drop-down field for a field... In reply to
Hi,

Ah, think I understand you now. I'd use something like this; (you'll probably wanna use Full_Name though, and not Name);

Code:
sub {
my $back = qq|<select name="catid">|;
my $tbl = $DB->table('Category');
$tbl->select_options('ORDER BY Full_Name DESC');
my $sth = $tbl->select();
while (my $hit = $sth->fetchrow_hashref) {
$back .= qq|<option value="$hit->{ID}">$hit->{Full_Name}</option>|;
}
$back .= q|</select>|;
}

..and call in search.html with;

<%global_name%>

Assuming I'm understanding what your asking for :D

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] Dynamically generate a drop-down field for a field... In reply to
Hi Andy,

thx, works great!

Kai
___________________________________________
http://www.westalgarve.de
http://www.portugalforum.org
http://www.portugal-links.de
> >