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!

Translate from ASP to ColdFusion

Status
Not open for further replies.

books1999

Programmer
Sep 10, 2003
4
MK
I have stumbled upon a program that creates javascript foldable menu in AsP.

I have tried unsucessfully to reproduce this code in CFML so I was wondering if someone on this forum would be able to do it.
My problem comes with the scripting technique used in ASP as opposed in ColdFusion. I was told that this is impossible to reproduce for CFML. Any opinions?

'procedure generates one node and recursively repeats for all children
sub DrawNode( Node )
dim rs, rsCildren
Dim NodeId, ParentID, Caption, Url, Target
'open node and read all fields to variables
set rs = dbConn.Execute("select * from dbo.treemenu where NodeID = " & Node )
NodeId = rs("NodeId")
ParentID = rs("ParentID")
Caption = QuoteAndCheckStr( rs("Caption") )
Url = QuoteAndCheckStr( rs("Url") )
Target = QuoteAndCheckStr( rs("Target") )
'write node to response
Response.Write(Space(Level * 4) & "[" & Caption & ", " & Url & ", " & Target)
rs.Close
set rs = nothing
'get all children
set rsCildren = dbConn.Execute("select * from dbo.treemenu where ParentID = " & Node )
if rsCildren.eof and rsCildren.bof then
'there is no children - close branch
Response.Write("]," & vbCrLf)
else
Level = Level + 1
Response.Write("," & vbCrLf)
'if children exists - call this sub recursively for each child
while not rsCildren.eof
call DrawNode( rsCildren("NodeId") )
rsCildren.MoveNext
wend
Level = Level - 1
Response.Write(Space(Level * 4) & "], " & vbCrLf)
end if
rsCildren.Close
set rsCildren = nothing
end sub
%>
 
Since the function involves running a query, and CFSCRIPT (the traditional way to build user-defined functions in ColdFusion) has no functional equivilent for queries, this would be impossible in straight CFML in any version before MX.

If you're running pre-MX, you'd have to write a java object, or interact with an ASP page (either as a web service, or via an include if possible).

But if you're running MX, you're in reasonably good shape... because it has the
Code:
<CFFUNCTION>
... which allows you to build user-defined functions that include standard CFML tags (like CFQUERY).
It would look something like:
warning... pseudo-code... not tested or confirmed
Code:
<CFSET request.level = 1>

<CFFUNCTION name=&quot;DrawNode&quot;>
   <CFSCRIPT>
     var nPrivateNodeID = 0;
     sCRLF = &quot;#Chr(13)##Chr(10)#&quot;;
   </CFSCRIPT>
   <CFARGUMENT name=&quot;Node&quot; type=&quot;numeric&quot; required=&quot;true&quot;>
   <CFSET nPrivateNodeID = Node>
   <CFQUERY name=&quot;qryParent&quot; ...>
      select *
      from dbo.treemenu 
      where NodeID = #nPrivateNodeID#
   </CFQUERY>
   <CFOUTPUT>#RepeatString(&quot; &quot;,(request.level * 4))# #qryParent.Caption#, #qryParent.URL#, #qryParent.Target#</CFOUTPUT>
   <CFQUERY name=&quot;qryChildren&quot; ...>
      select *
      from dbo.treemenu 
      where ParentID = #nPrivateNodeID#
   </CFQUERY>
   <CFOUTPUT>,#sCRLF#</CFOUTPUT>
   <CFIF qryChildren.RecordCount GT 0>
      <!--- there are children --->
      <CFSET request.level = request.level + 1>
      <CFLOOP query=&quot;qryChildren&quot;>
         <CFSET DrawNode(qryChildren.NodeID)>
      </CFLOOP>
      <!--- not at all sure what this is trying to do --->
      <CFSET request.level = request.level - 1>
      <CFOUTPUT>#RepeatString(&quot; &quot;,(request.level * 4))#], #sCRLF#</CFOUTPUT>
   </CFIF>
</CFFUNCTION>


-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top