giovedì 5 giugno 2008

Creating a select for all nations using asp.

A little web development parenthesis.
Yesterday I needed to ass to a website's registration form the choice of nation, including all existing nations.
I wanted to make a "select", with the possibility to preselect a specific nation, or the user previous choice.
Since I didn't find any available code online, I'll post here my solution, using asp and a text file.

You just need an ASP server (but the code can be easily adapted to other languages) and a text file with the nations you'd like to show, one nation per line. In my site I used this file, including all nations in Italian.
Whit asp (or your server side languages) you just need to read the file line by line and build your select.
Here's the code I used:

'variable holding preselected value. Could be retrieved from user's recordset
Nation = "Italy"

'filestream opening
filename = Server.MapPath("/Nations.txt")
Set objFile = CreateObject("Scripting.FileSystemObject")
Set filestream = objFile.OpenTextFile(filename, 1, true)

'variable holding values read from textfile
Dim ActualNation
ActualNation = filestream.ReadLine()

'printing select start, and first option (might be set as default)
<select name="Nation" id="Nation">
<option value="0">
Select your nation:
'cycle to build the different options and set selected item
DO WHILE NOT filestream.AtEndOfStream %>
<option value="<%=ActualNation%>" <%IF Nazione=ActualNation THEN %> selected="selected" <%END IF%> ><%=ActualNation%></option>
<% ActualNation = filestream.ReadLine()
'select closing tag
'closing filestream
Set filestream = Nothing
Set objFile = Nothing

And here's the result:

Hope this can be useful to someone.. Please provide comments, suggestions and improvements!

PS: still no news from protector.. But a new great game demo is out there:

site by  wellD, web-communication, Lugano