Gossamer Forum
Home : Products : Gossamer Links : Discussions :

"select box" mystery

Quote Reply
"select box" mystery
well... here is another one:

if i create a "select" type field in the database (ie. list of countries - there are 240 of them), HOW will i get this select list to the user? i belive it can be done with the <%loop%> tag, but i just wasn't able to find out how. if you are going to respond, please take it step by step - i'm quite a beginner.

thank you Wink

robo
Quote Reply
Re: [Robo] "select box" mystery In reply to
Hi,

Try this as a global (Thanks Paul for finishing this one for me also):

Code:
sub {
my $return = q|<select name="Foo">|;
my @list = $DB->table('Countries')->select->fetchall_list;

$return .= map { q|<option>$_</option>| } @list;
$return .= q|</select>|;
return $return;
}


If you have not made a global before.... let me know.

Basically you have to give this global a name when you enter it, say select_country... then the tag you would place would be <%select_global%>.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jul 18, 2002, 11:26 AM
Quote Reply
Re: [Ian] "select box" mystery In reply to
Hi Ian Wink

Thank you for your reply! I believe that we are getting closer to the solution! Anyway - it is not working for me at the moment...

1) If I just put your code into the global (which name would be for example select_country) and then if I call that global (with <%select_country%>), the only thing that come up in the HTML, is just the code itself - it hasn't been executed!!

2) If I try to do it your way (naming the global select_country and then call it with <%select_global%>), I just get an error Unknown Tag: 'select_global'

3) The code you have provided seems correct, BUT it seems it wouldn't display previously selected option! I mean, if I put it in the modify page, it will not be set to the value which is in the database. So I think we need a code that would not only list all the possibilities, but that will also select the value which is already in the database / or which will set the list to the default value, if there is no value in the database.

Perhaps somebody from the Gossamer's can help us with that?

Thank you for everything and have a nice day Sly

Robo
Quote Reply
Re: [Robo] "select box" mystery In reply to
3) can be satifisfied by the following code. (I'm sure Ian knows the answer but since I'm here before he is... Wink

Code:
sub {

my $element_name = 'Foo'; # set the control's name here
my $default_value = 'Uzbekistan'; # and the default country you'd like to use

my $return = q|<select name="$element_name">|;
my @list = $DB->table('Countries')->select->fetchall_list;

my $tags = GT::Template->tags || {};
my $selected = $tags->{$element_name} || $default_value;
$selected = { map {($_=>1)} ref $selected ? @$selected : $selected };

foreach my $country ( @list ) {
my $ischosen = $selected->{$country} ? " selected" : "";
$return .= qq|<option$ischosen>$country</option>|;
}

$return .= q|</select>|;
return $return;
}

edit: Whoops, had to fixup the code a little

Last edited by:

Aki: Jul 19, 2002, 1:20 AM
Post deleted by Robo In reply to
Quote Reply
Re: [Robo] "select box" mystery In reply to
Yes, he is referring to template globals.

Cut and past the code into the blank global box (towards the bottom), and put the name of the global in the blank smaller box just to the left of it, then click 'update'.


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jul 19, 2002, 1:13 AM
Quote Reply
Re: [Aki] "select box" mystery In reply to
Thank you very much Aki!

The code you've provided seems much more better!! It seems it should deal without any problems the default and selected values.
But anyway -- it is still erroneous! I got this error message:

GT::SQL (14281): File '/mydirectory/admin/defs/directorio_Countries.def' does not exist or the permissions are set incorrectly at /mydirectory/admin/GT/SQL/Base.pm line 62.

I think I understand it: there is NO 'Coutries' table! 'Countries' is just one column in the 'Links' table!! But in the code you are trying to open 'Countries' table:

my @list = $DB->table('Countries')->select->fetchall_list;

I was also looking at the server and there is NO directorio_Countries.def file. If I tried to change in your code 'Countries' to 'Links', I GOT THE LIST, but it was list of ALL the columns and all data in Links database.

I think we are very close to the final code. Please clear this up.

Thank you very much, you're both ligting fast Wink

Robo
Quote Reply
Re: [Ian] "select box" mystery In reply to
Thank you Ian for clearing this up, TEMPLATE GLOBALS are not the mystery to me no more. Now I just need the final changes to the provided code (see previous post). Thank you! Tongue Robo
Quote Reply
Re: [Robo] "select box" mystery In reply to
$DB->table('Links')->select( 'Countries' )->fetchall_list

Last edited by:

Paul: Jul 19, 2002, 2:05 AM
Quote Reply
Re: [Paul] "select box" mystery In reply to
Thank you Paul Smile

NOW it seems it can connect to the database.
BUT I still don't get the result I need Unsure My code looks like this:

Code:


sub {
my $element_name = 'Pais'; # set the control's name here
my $default_value = 'España'; # and the default country you'd like to use

my $return = q|<select name="$element_name">|;
my @list = $DB->table('Links')->select('Pais')->fetchall_list;

my $tags = GT::Template->tags || {};
my $selected = $tags->{$element_name} || $default_value;
$selected = { map {($_=>1)} ref $selected ? @$selected : $selected };

foreach my $country ( @list ) {
my $ischosen = $selected->{$country} ? " selected" : "";
$return .= qq|<option>$country</option>|;
}

$return .= q|</select>|;
return $return;
}


'Pais' is the name of the column I am referring to (Pais means 'Country' in spanish). If I use the previsous code, I get this result in HTML code:

Code:
<select name="$element_name">
<option></option>
<option></option>
<option>España</option>
</select>
Which is indeed quite strange!! At least it should put the "Pais" word in the 'name' field if nothing else...

Any suggestions? Thanks to you all Sly

Robo
Quote Reply
Re: [Robo] "select box" mystery In reply to
I wonder if provided code is working fine for YOU? Have you tried it?
Quote Reply
Re: [Robo] "select box" mystery In reply to
As I think about it, I think that the best would be if you could provide me with piece of working code - code which is installed somewhere on your page and which is working fine - if it is working for you, it should be working for me without any problems, am I right?

Sly Robo
Quote Reply
Re: [Robo] "select box" mystery In reply to
You need a table "Countries" where all the countries are listed to get the above code working....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] "select box" mystery In reply to
Really ??!

And how can I create new table?
Quote Reply
Re: [Robo] "select box" mystery In reply to
Sorry about the confusion. So many of us are programmers and all we're usually looking for is a basic skeleton that we can hack... and seeing as you sounded like you had a table and knew perl fairly well... assumptions assumption Blush

Anyways, first question for you. Where is this list of countries? Do you have it on the server in a database or in a file?
Quote Reply
Re: [Aki] "select box" mystery In reply to
Hello Aki!

Thank you for understanding us - not-perl-programmers...

The respond to your question is:

I have created new "Pais" column in the "Links" table (I've made it with admin's Table Editor):

Column Name: Pais
Column Type: ENUM
Column Index: None
Column Values: Albania Alemania Andorra (withou commas, each country on a new line)
Not Null: No
Default: España
Form Display: País
Form Type: Select
Form Names: Albania Alemania Andorra (withou commas, each country on a new line)
Form Values: Albania Alemania Andorra (withou commas, each country on a new line)
Search Weight: 1

Then... on the admin side everything works fine - if I go to add new link I am presented with the perfect select box. Thing is to get the same to the users Unimpressed

I hope I've responded well your first question. Thank you for your concern Sly

Robo

Last edited by:

Robo: Jul 20, 2002, 12:18 AM
Quote Reply
Re: [Robo] "select box" mystery In reply to
thanks for the info... try this:

Code:
sub {

my $element_name = 'Pais'; # set the control's name here
my $default_value = 'Espana'; # and the default country you'd like to use
my @list = qw(
Albania
Alemania
Andorra
); # add your countries here. One per line is ok.

my $return = q|<select name="$element_name">|;

my $tags = shift || {};
my $selected = $tags->{$element_name} || $default_value;
$selected = { map {($_=>1)} ref $selected ? @$selected : $selected };

foreach my $country ( @list ) {
my $ischosen = $selected->{$country} ? " selected" : "";
$return .= qq|<option$ischosen>$country</option>|;
}

$return .= q|</select>|;

return $return;
}
Quote Reply
Re: [Aki] "select box" mystery In reply to
Hello Aki Sly

You're lighting fast man! Thank you!
This code you've sent is almost working. We are almost already there! The only thing that needs to be corrected is one small bug:

When some country name consists of more than one word (ie. Great Britain), your code treats it as TWO countries - in this example it would be "Great" and "Britain". I hope you can fix this easily.

Thank you for being there Tongue

Robo

Last edited by:

Robo: Jul 20, 2002, 8:41 AM
Quote Reply
Re: [Robo] "select box" mystery In reply to
Oh Shucks. You'll need some more code then. Add the following line to the global just after the my @list = part.

Code:
grep { tr,_, ,;0 } @list;

Now, for every country you have in the list, if there is a space, change that to a "_" (an understore). The code just above translates all understores to spaces.

Hope this helps,
Aki
Quote Reply
Re: [Aki] "select box" mystery In reply to
Thank you Aki for everything!! It is working right now!!! Here is the final code for anybody who may be interested to get it:

Code:
sub { my $element_name = 'Pais'; # set the control's name here
my $default_value = 'xyzty'; # and the default country you'd like to use
my @list = qw(
Albania
Alemania
Andorra
Angola
Anguilla
Antártida
Antigua_y_Barbuda
Antillas_Neerlandesas
Arabia_Saudí
Argelia
Argentina
Aruba
Australia
Austria
Azerbaiyán
Azores
Bahamas
Bahrein
Bangladesh
Barbados
Bélgica
Belice
Benín
Bermudas
Bielorrusia
Bolivia
Bonaire
Bosnia-Herzegovina
Botswana
Brasil
Brunei
Bulgaria
Burkina_Faso
Burundi
Bután
Cabo_Verde
Camboya
Camerún
Canadá
Chad
Chile
China
Chipre
Ciudad_del_Vaticano
Colombia
Commonwealth_de_las_Marianas_del_Norte
Congo
Corea_del_Sur
Costa_de_Marfil
Costa_Rica
Croacia
Curaçao
Dinamarca
Djibouti
Dominica
Ecuador
Egipto
El_Salvador
Emiratos_Árabes_Unidos
Eritrea
Escocia
Eslovaquia
Eslovenia
España
Estados_Federados_de_Micronesia
Estados_Unidos
Estonia
Etiopía
Fiji
Filipinas
Finlandia
Francia
Gabón
Gales
Gambia
Georgia
Ghana
Gibraltar
Granada
Grecia
Groenlandia
Guadalupe
Guam
Guatemala
Guayana_Francesa
Guinea
Guinea_Bissau
Guinea_Ecuatorial
Guyana
Haití
Holanda
Honduras
Hong_Kong
Hungría
India
Indonesia
Inglaterra
Irak
Irán
Irlanda
Irlanda_del_Norte
Islandia
Islas_Caimán
Islas_Canarías
Islas_Cook
Islas_del_Canal
Islas_Feroe
Islas_Marshall
Islas_Norfolk
Islas_Salomón
Islas_Turks_y_Caicos
Islas_Vírgenes_Americanas
Islas_Vírgenes_Británicas
Israel
Italia
Jamaica
Japón
Jordania
Kazajstán
Kenia
Kirguizistán
Kiribati
Kosrae
Kuwait
Laos
Lesoto
Letonia
Líbano
Liberia
Libia
Liechtenstein
Lituania
Luxemburgo
Macao
Macedonia
Madagascar
Madeira
Malasia
Malawi
Maldivas
Malí
Malta
Marruecos
Martinica
Mauricio
Mauritania
México
Moldovia
Mónaco
Montserrat
Mozambique
Myanmar
Namibia
Nepal
Nicaragua
Níger
Nigeria
Noruega
Nueva_Caledonia
Nueva_Zelanda
Omán
Paises_Bajos
Pakistán
Palau
Panamá
Papúa-Nueva_Guinea
Paraguay
Perú
Polinesia_Francesa
Polonia
Ponatpe
Portugal
Puerto_Rico
Qatar
Reino_Unido
República_Centroafricana
República_Checa
República_Democrática_del_Congo
República_Dominicana
Reunión
Rota
Ruanda
Rumania
Rusia
Saba
Saipan
Samoa_Americana
Senegal
Seychelles
Sierra_Leona
Singapur
Siria
Sri_Lanka
St._Barthelemy
St._Christopher
St._Croix
St._Eustatius
St._John
St._Kitts_y_Nevis
St._Lucia
St._Maarten
St._Martin
St._Thomas
St._Vincente_y_las_Granadinas
Suazilandia
Sudáfrica
Sudán
Suecia
Suiza
Surinam
Tahití
Tailandia
Taiwán
Tanzania
Tayikistán
Tinian
Togo
Tonga
Tortola
Trinidad_y_Tobago
Truk
Tunicia
Turquía
Tuvalu
Ucrania
Uganda
Union_Island
Uruguay
Uzbekistán
Vanuatu
Venezuela
Vietnam
Virgen_Gorda
Wake_Island
Wallis_y_Futuna_Islands
Western_Samoa
Yap
Yemen
Zambia
Zimbabwe
);
grep { tr,_, ,;0 } @list;
# add your countries here. One per line is ok. my $return = q|<select class="pais" name="Pais"><option value="">---</option>|; my $tags = shift || {};
my $selected = $tags->{$element_name} || $default_value;
$selected = { map {($_=>1)} ref $selected ? @$selected : $selected };



foreach my $country ( @list ) {
my $ischosen = $selected->{$country} ? " selected" : "";
$return .= qq|<option$ischosen>$country</option>|;
}



$return .= q|</select>|;

return $return;
}