Question

Je voudrais gérer une ligne à partir d'une requête par une fonction, où je passe la ligne comme structure.

idéalement...

<cfloop query="myquery">
 #myfunction(#row#)#
</cfloop>

Je pourrais l'installer comme ça aussi ...

<cfloop query="myquery">
 #myfunction(#col1#,#col2#,#col3#,#col4#)#
</cfloop>

Mais je ne veux pas. Je n'ai pas pu trouver un moyen simple d'extraire une ligne, mais je pensais que je demanderais.

Était-ce utile?

La solution

Adobe Coldfusion a maintenant une fonction nommée QueryGetRow qui convertit une ligne d'une requête en structure.

Addenda

Cette fonction n'est pas disponible sur Coldfusion 9 et plus tôt.

Autres conseils

trouvé une solution un peu plus élégante, pour une seule rangée

<cfscript>
    function GetQueryRow(query, rowNumber) {
        var i = 0;
        var rowData = StructNew();
        var cols    = ListToArray(query.columnList);
        for (i = 1; i lte ArrayLen(cols); i = i + 1) {
            rowData[cols[i]] = query[cols[i]][rowNumber];
        }
        return rowData;
    }
</cfscript>

Ben Nadel a publié un article de blog à ce sujet où il donne un exemple UDF qui convertit une requête en structure, et il accepte un argument de ligne facultatif qui vous permet de tourner une seule ligne dans cette requête en structure. Regarde ici.

Ceci est la classe du site de Ben sans les commentaires

<--- --------------------------------------------------------------------------------------- ----

    Blog Entry:
    Ask Ben: Converting A Query To A Struct

    Author:
    Ben Nadel / Kinky Solutions

    Link:
    http://www.bennadel.com/index.cfm?event=blog.view&id=149

    Date Posted:
    Jul 19, 2006 at 7:32 AM

---- --------------------------------------------------------------------------------------- --->


<cffunction name="QueryToStruct" access="public" returntype="any" output="false"
    hint="Converts an entire query or the given record to a struct. This might return a structure (single record) or an array of structures.">
    <cfargument name="Query" type="query" required="true" />
    <cfargument name="Row" type="numeric" required="false" default="0" />

    <cfscript>
        var LOCAL = StructNew();
        if (ARGUMENTS.Row){
            LOCAL.FromIndex = ARGUMENTS.Row;
            LOCAL.ToIndex = ARGUMENTS.Row;
        } else {
            LOCAL.FromIndex = 1;
            LOCAL.ToIndex = ARGUMENTS.Query.RecordCount;
        }
        LOCAL.Columns = ListToArray( ARGUMENTS.Query.ColumnList );
        LOCAL.ColumnCount = ArrayLen( LOCAL.Columns );
        LOCAL.DataArray = ArrayNew( 1 );
        for (LOCAL.RowIndex = LOCAL.FromIndex ; LOCAL.RowIndex LTE LOCAL.ToIndex ; LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
            ArrayAppend( LOCAL.DataArray, StructNew() );
            LOCAL.DataArrayIndex = ArrayLen( LOCAL.DataArray );
            for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE LOCAL.ColumnCount ; LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
                LOCAL.ColumnName = LOCAL.Columns[ LOCAL.ColumnIndex ];
                LOCAL.DataArray[ LOCAL.DataArrayIndex ][ LOCAL.ColumnName ] = ARGUMENTS.Query[ LOCAL.ColumnName ][ LOCAL.RowIndex ];
            }
        }
        if (ARGUMENTS.Row){
            return( LOCAL.DataArray[ 1 ] );
        } else {
            return( LOCAL.DataArray );
        }
    </cfscript>
</cffunction>

usage...

<!--- Convert the entire query to an array of structures. --->
<cfset arrGirls = QueryToStruct( qGirls ) />

<!--- Convert the second record to a structure. --->
<cfset objGirl = QueryToStruct( qGirls, 2 ) />

Une autre solution serait:

function QueryToStruct(query){
    var cols    = ListToArray(query.columnList);
    var salida  = query.map(function(v=0,i,a){
                       return {'#cols[1]#':v};
                  });  
    return ValueArray(salida,'#cols[1]#');
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top