Coldfusion - variable field name when looping through database query results

前端 未结 2 1313
无人及你
无人及你 2020-11-30 14:11

I have a set of column names in a table - e.g. foo1, foo2, foo3, foo4. I want to refer to these column names dynamically through a loop:



        
2条回答
  •  青春惊慌失措
    2020-11-30 14:49

    Don't use Evaluate() for things like that! It's slow and should be avoided.

    
      
    
    

    Or, if you like:

    
      
    
    

    Evaluate() is for evaluating bits of code. Don't use it for things that can be solved more elegantly in language-integrated, more appropriate ways.

    EDIT:

    When accessing Query objects with the "angle bracket"-syntax, you must append the (1-based) row number index (query["foo#i#"][RowNum]). When using the traditional "dot"-syntax (query.foo1), the current row is implicit.

    To access the current row explicitly, use the QueryObject.CurrentRow property. But it could be any positive integer up to QueryObject.RecordCount. A range check is advised for anything other than CurrentRow.

    This opens an interesting field: You can start to use query objects with "random access". Previously (before CFMX) all you could do was iterate them from start to end, pulling out the things that you look for. Now it's like a nested struct/array data structure that you can use in different ways.

提交回复
热议问题