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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refining function arguments

Status
Not open for further replies.

snowneil

Programmer
Mar 22, 2006
40
GB
The below shows a couple of functions to create the options for a select box. They don't cover much but still working on them. The arguments beginning with ex don't seem like the best way to do it. Anyone have any ideas on how to refine these?
Code:
* this method gets data from 1 lookup table for use with a simple select box
* arguments provided are id = primary key from the table, value = description from the table, 
* tableName = name of table, database = ADODB.Connection object, 
* whereClauses = any string of where clauses to add to the sql,
* order = true for ascending and false for descending.
**/
function getOptionData(id,value,tableName,database,whereClauses,order) { 
	var sql, sqlRS;
	var rsArray = new Array();
	var counter = 0;
	var sql = "SELECT " + id + "," + value + " FROM " + tableName + " WHERE " + value + "<> '' " + whereClauses;
	if(order) { sql += " ORDER BY " + value; } else { sql += " ORDER BY " + value + " DESC"; }
	sqlRS = database.Execute(sql);
	while(!sqlRS.EOF)
	{
		rsArray[counter] = new Array();
		rsArray[counter]["id"] = sqlRS(id).Value;
		rsArray[counter]["value"] = sqlRS(value).Value; 
		counter++;
		sqlRS.MoveNext();
	}
	return rsArray;
}

/**
* method creates options for a select box. Arguments: id = id of select box for use within the session array,
* lookupData = recordset array obtained from using the getOptionData method, sessionArray = name of the session array used,
* exDataArray = name of two dimensional array that holds the data of an existing record to display in the select box,
* exCount = number to use with exData for it's first dimension eg. exData[0]["valueName"],
* exCheck = if true then check the exData array for a value to display otherwise don't,
* allowBlank = true for allowing an empty value in box, false for not allowing it.
**/
function createOptions(id,lookupData,sessionArray,exDataArray,exCount,exCheck,allowBlank) { 
	if(allowBlank) { 
		Response.Write("<option value='' ");
		if (Session.Contents(sessionArray)[id] == ""){ Response.Write("Selected"); }
		Response.Write("></option>");
	}
	for(i in lookupData)
	{
		Response.Write("<option value='" + lookupData[i]["id"] + "' ");
		if (Session.Contents(sessionArray)[id] == null) { 
			if(exCheck) { if (lookupData[i]["id"] == exDataArray[exCount][id]) { Response.Write("selected") } } 
		} else { if (Session.Contents(sessionArray)[id] == lookupData[i]["id"]){ Response.Write("Selected"); } }
		Response.Write(">" + lookupData[i]["value"] + "</option>");
	}
}

How i use them. variables con, sessionName, cmpArray already defined.
Code:
<select id="title" name="title" tabindex="1">
<%
var titleRS = getOptionData("id","title","lookup_titles",con,"",true);
createOptions("title",titleRS,sessionName,"","",false,true);
%>
</select>

<select id="affinity" name="affinity" tabindex="4">
<%
var affClause = "AND active = 'Yes'";
var affRS = getOptionData("id","affinity","lookup_affinity",con,affClause,true);
createOptions("affinity",affRS,sessionName,cmpArray,0,true,true);
%>
</select>
 
ex arguments being these ones
function createOptions(id,lookupData,sessionArray,exDataArray,exCount,exCheck,allowBlank)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top