Arg, me mateys!



  • This is the most creative way to make a dropdown list from a database query EVER. arg1 is a result set, arg2 is the name of the field from which to get the value, and arg3 is a very special formatted string containing a list of fields from which to retrieve information to be displayed in the option tags. I had to investigate when I first encountered code that looked like this:

    <%
    Set dropdown9 = New DropClass
    dropdown9.makeList DBData1.data, "nName_ID", "v-cLast_Name-e & *, * & v-cFirst_Name-e & * * & v-cMiddle_Name-e"
    %>
    

    Now here's the actual method that handles the above code.

    <%
    function makeList(arg1,arg2,arg3)
    	arg3 = Replace(arg3,"v-","arg1(*")
    	arg3 = Replace(arg3,"-e","*)")
    	arg3 = Replace(arg3,"*",chr(34))
    
    On Error Resume Next
    
    do until arg1.eof
    	&apos; length is an instance variable that must be
    	&apos; set before calling the makeList function. -djork
    	if not isempty(length) then
    		optionout = Left(Eval(arg3),length)
    	else
    		optionout = Eval(arg3)
    	end if
    	
    	&apos; output is an instance variable to be retrieved later -djork
    	output = output &amp; &quot;&lt;option value=&apos;&quot; &amp; arg1(arg2) &amp; &quot;&apos;&quot;
    
    	if arg1(arg2) = defaultValue then
    		output = output &amp; &quot; SELECTED&quot;
    	end if
    
    	output = output &amp; &quot;&gt;&quot; &amp; optionout &amp; &quot;&lt;/option&gt;&quot; &amp; chr(13)
    
    	arg1.MoveNext
    loop
    
    arg1.MoveFirst
    

    end function
    %>

    I'll leave it up to you to decipher what ends up making this piece of black magic work.



  • At the point where you're Eval()ing, why bother with all the inane formatting rules with "v-" and "-e", and simply have arg3 be the code to evaluate in the first place?

    My guess is someone didn't know how to embed quotes.

    I'm also curious about the length -- neat that it's not passed in as a param.  But I don't figure how it can be used reliably, since the length of arg3 is going to increase by 4 for every occurrence of "v-" in it. 

     --Colin McGuigan



  • "v-cLast_Name-e & *, * & v-cFirst_Name-e & * * & v-cMiddle_Name-e"

    After first 3 lines of code, becomes (slashes used to delimit)

    /DBData1.data("cLast_Name") & ", " & DBData1.data("cFirst_Name") & " " & DBData1.data("cMiddle_Name")/

    I can see how it works from here.  Wow, that's creative.  And stupid. 


Log in to reply