please bare with me here:
<%
dim VAR(2)
VAR(1)="nba"
VAR(2)="mlb"
sql_getVAR = "SELECT DISTINCT pcity FROM VAR"
Set getVAR = Conn.Execute(sql_getVAR) %>
I want several table according to the sport (nba,mlb, nfl etc...) I thought if i put the above code on an asp page (page02.asp) and then specified VAR on page01.asp with the link for NBA being /teams_02.asp?VAR=1 it would display the fields from the "nba" table.
Of course it did not saying the error was that VAR was not defined.
Can someone tell me what I am doin wrong or if you understand it, explain a better way to me. Thanks.
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
Array index starts at 0 not 1.
Code:
<%
dim VAR(1) '1 means the upper bound of the array (the last index). so an upper bound of 1 means 2 (ubound + 1) elements.
VAR(0)="nba"
VAR(1)="mlb"
sql_getVAR = "SELECT DISTINCT pcity FROM " & Join(VAR, ",")
Set getVAR = Conn.Execute(sql_getVAR)
%>
<%
select case Request.QueryString("VAR")
case "1"
sql_getVAR = "SELECT DISTINCT pcity FROM NBA"
case "2"
sql_getVAR = "SELECT DISTINCT pcity FROM MLB"
case "3"
sql_getVAR = "SELECT DISTINCT pcity FROM NFL"
case else
'invalid VAR, stop processing
response.write "Invalid request!"
response.end
'or you may want to query a default table instead
'sql_getVAR = "SELECT DISTINCT pcity FROM NBA"
end select
response.write "sql:" & sql_getVAR
%>
------------------
select case Request.QueryString("VAR")
case "1"
sql_getVAR = "SELECT DISTINCT pcity FROM NBA"
case "10000000000"
.....
---------------
my code is just a shortcut for it >>>
const varS ="mlb,nba,nlf,as many as you like,...."
sql_getVAR = "SELECT DISTINCT pcity FROM " + + split(varS,",")(cint(Request.QueryString("var")))
you just split on the coma and with cint(Request.QueryString("var")) you get an integer to return the value of your array
with "mlb,nba,nlf"
mlb = 0
nba = 1
nlf =2
imagine you have 1000 possibilities !! ... case 1 to case 1000 is not realist
split(myArrayofValues,",")(number) will return it in just one line of code
<%
dim dbtable, index
dbtable = Array("nba", "mlb", "nfl", "...")
index = Request.QueryString("VAR")
if isnumeric(index) then
index = CInt(index)
else
index = 0
end if
if index < 0 or index > ubound(dbtable) then 'not within range
index = 0
end if
sql_getVAR = "SELECT DISTINCT pcity FROM " & dbtable(index)
response.write "sql:" & sql_getVAR
%>