Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically create variables for a page 2

Status
Not open for further replies.

ironhide1975

Programmer
Feb 25, 2003
451
US
Not sure if this can be done, but I am working with a website with multiple languages. I want to have the site have a predefined list of variables for each web page. When the page is execute it will go to the database and pull back the appropriate words for that language.

I am having a problem at the part where you pull back the variable and specify what it is. Here is my code

Code:
WordItems = WordItems & ",TLanguage,TRegisterName,TLogin,TLogout,TPassword,TSelect,TEdit,TProductSearch"

MyArray = split(WordItems,",")
For I=0 to Ubound(MyArray) 
TempSubXNoID=myArray(I)
	
'Roll Through Carry Thru Sub Numbers====================================
sSQLC = "SELECT * FROM [GENERAL-WordItems_Master] WHERE Abbreviation = '"& TempSubXNoID &"'"
set rsGetC = Conn.Execute(sSQLC)
TempItem = rsGetC("WordName")
myArray(I) = TempItem
	
'now how do I say whatever the name of TempSubXnoID is, please set it to TempItem?			
Next

So say in the database you had the variable TLanguage and the fkLanguage = 1 and the word is 'Language'

Any help is greatily appreciated.

 
i'm still not sure what you mean, you could trim TLanguage

response.write( mid(myArray(I),2) )

which will give you Language
 
I need to be able to set a variable dynamically per ASP page for example. Say on the page I had to do a

response.write TSomeWord

At the start of the page I put a array of words I need to grab from the database, say TSomeWord,TAnotherWord etc... I query the database and it tells me TSomeWorld is 'Hey Good Morning'. Now how to specify in the webpage (dynamically) that

TSomeWord = 'Hey Good Morning'


 
OK, so you have an array of abbreviation elements.

You loop through the array and for each element you search a database table for all fields of all rows that match the abbreviation element.

From the first row of the returned recordset, you take the value of the field WordName and overwrite the abbreviation element that you used to search with.

You repeat for each element of the array.

I don't understand the question.

 
The item has to get the actual name of the element from the array and then tell the ASP page that this element = whats in the database.

So I have an array

my array = "T1,T2,T3"

and in the database it says

T1 = "Yo"

I need the ASP Page then to say

T1 = "Yo"

 
It looks like what you have will work if you simply wish to REPLACE each abbreviation with the word from the first database row it matches.

Later in the code when you get around to actually using the values, assuming a zero-bound array, you can do this:
<html>
<head>
<title>
Test Page
</title>
</head>
<body>
The value of the first element is:
<%= myArray(0) %>
<br>
The value of the second element is:
<%= myArray(1) %>
</body>
</html>
 
Maybe you could load it into a dictionary object if you don't like the array.
 
I can't do a response.write with the array.

I have to do

<%=T1%>



 
I could do this but I already have variables placed around the page from the previous coding. Instead of having a huge include page with all the different rendetions of T1 with different languages, I want to have it dynamically pulled from the database.

 
So you don't want to fix the messy code, you just want to make working with it less tedious!
 
Well I agree the code would work better, however more importantly I need to know if its possible to dynamically generate variables for an ASP page.

 
Yes. It's called an array. It may have been mentioned before

The simple thing is instead of trying to fudge something together from previous coding it's easr to start with a clean sheet.

start with the sql. WordItems is already a CSV so this
Code:
sSQLC = "SELECT WordName FROM [GENERAL-WordItems_Master] WHERE Abbreviation IN '"& WordItems &"'"
set rsGetC = Conn.Execute(sSQLC)
myArray = rsGetC.GetRows()
will give an array of all items pulled from the database

But it appears that you are after an associative array which in ASP vbScript is a dictionary object (as Sheco said) Now if that is correct we can soon work out some code for that.

Code:
set assocArray = server.createobject("Scripting dictionary")
MyArray = split(WordItems,",")
For each I in MyArray
 
sSQLC = "SELECT WordName FROM [GENERAL-WordItems_Master] WHERE Abbreviation = '"& i &"'"
set rsGetC = Conn.Execute(sSQLC)
assocArray.add i,rsGetC("WordName")
next
That should give you a dictionary where

response.write assocArray.item("TLanguage") will display the database content.

not tested BTW coding at the keyboard







Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
Nightclub counting systems

So long, and thanks for all the fish.
 
ironhide,

I think I understand what you were initially asking, to which I've provided an example below, but I would recommend scrapping this idea if you can and following Chris & Sheco's suggestion to use a dictionary object, as it is a much more controllable method.


Code:
execute( rsGetC("WordName") & " = " & chr(34) & rsGetC("WordValue") & chr(34) )

Or simply: execute( "myVariableName = myVariableValue" )

Put this into a loop and you will be able to dynamically create all the variables in your page (useful if you cannot change anything other than the variable generation).

I would still recommend re-designing your page to use the dictionary object though.

I would also suggest that you reconsider your direct database access for every page request. This can get quite resource intensive for high hit sites. You may want to declare the code pages (resource files) in the dictionary objects with a global scope - maybe populated in your Application_OnStart event. Then when a user logs in or sets their language (either dynamically or manually) they update their session variable with a link to the relevant global dictionary object - which then simply is refered to as per Chris's post above. As the translation sets will be stored in memory (once for each language) you will have the memory overhead of the translation sets(once), and processing from memory is much more efficient than from a DB.

This kind of goes full circle back to the way you have now, except rather than use an include file, you could use global variables, and populate them from the database which can store and manage the values(if you are trying to make the translations more accessible).


Anyway, enough rambling.. (-:
Damian

A smile is worth a thousand kind words. So smile, it's easy! :)
 
I apologize for my knowledge of ASP is somewhat backwards as I've learned it through bits and pieces. I am unsure what a dictionary object is. (BTW can anyone recommend a
good HTML/ASP Classic editor? I started with GoLive/Dreamweaver and am now using HTMLKit.)

I tried Chris's code and I got the following error.

Code:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14' 

[Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near 'TLanguage,TRegisterName,TLogin,TLogout,TPassword,TSelect,TEdit,TProductSearch,BSearch,BAbout,BHome,BCompany,BP'.

I tried Damian's code with my original code and got

Code:
Item cannot be found in the collection corresponding to the requested name or ordinal.

From what I can tell I think Damian has the right idea. It may not be the most effective method, but its a work in progress for now as I basically am tearing out the plumbing and re-working it.

Continued help is appreciated.


 
Got it!

Code:
WordItems = "TLanguage,TRegisterName,TLogin,TLogout,TPassword,TSelect,TEdit,TProductSearch,BSearch,BAbout,BHome,BCompany,BProducts,BSuppliers,BTechnology,BEmployment,BSitemap,BContact,BCopyright,BTerms," & WordItems

MyArray = split(WordItems,",")
For I=0 to Ubound(MyArray) 
TempSubXNoID = myArray(I)
	
sSQLC = "SELECT * FROM [GENERAL-WordItems_Master] WHERE Abbreviation = '"& TempSubXNoID &"'"
set rsGetC = Conn.Execute(sSQLC)
TempItem = rsGetC("WordName")
myArray(I) = TempItem
			
execute( rsGetC("Abbreviation") & " = " & chr(34) & rsGetC("WordName") & chr(34) )	
				
Next

 
I'm having a problem with my code and unsure on how to handle this. When I execute my code, it will sometimes not bring back all the day for the fields. This usually occurs somewhere where there is two text fields in the table. If I chose to pass one variable back, the item will work fine,

selectName = "Problem" works fine.

selectName = "Problem,Definition" will bring back Definition but not Problem data.

The SQL query works fine in Enterprise Manager, but when I try to execute it in a webpage, I'm missing info.

please help...

Code:
<%
sSQL2 = "SELECT " & selectName &" FROM vwCARs WHERE PlantID = 1 AND Active = 1 ORDER BY " & Sort	
 
Set rsObj = Conn2.Execute(sSQL2)
IF NOT rsObj.BOF THEN
		
For Each F In rsObj.Fields 
 Head = Head & NextSetH & F.Name 
Next 
 Head = Head

MainBlock = MainBlock &  rsObj.GetString(,,NextSet,NextStep,"")
%>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top