Highlighted

How to loop through struct query result?

Engaged ,
Dec 05, 2017

Copy link to clipboard

Copied

This is what I have.

<cfset inactivePagesReport = Server.CommonSpot.api.getObject('reports')>

<cfset inactivePages = inactivePagesReport.getInactivePages(0)>

<cfdump var="#inactivePages#">

struct-query.png

So, how do I loop through each row and only output certain columns like ID, Name, Link columns?

Any help is much appreciated.

Here's how I got it work.

<cfloop query="#inactivePages.ResultQuery#">

     <cfoutput >

          Title:  #subsiteurl#<br />

     </cfoutput>

</cfloop>

Views

697

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to loop through struct query result?

Engaged ,
Dec 05, 2017

Copy link to clipboard

Copied

This is what I have.

<cfset inactivePagesReport = Server.CommonSpot.api.getObject('reports')>

<cfset inactivePages = inactivePagesReport.getInactivePages(0)>

<cfdump var="#inactivePages#">

struct-query.png

So, how do I loop through each row and only output certain columns like ID, Name, Link columns?

Any help is much appreciated.

Here's how I got it work.

<cfloop query="#inactivePages.ResultQuery#">

     <cfoutput >

          Title:  #subsiteurl#<br />

     </cfoutput>

</cfloop>

Views

698

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 05, 2017 0
Engaged ,
Dec 05, 2017

Copy link to clipboard

Copied

Some options:

<cfoutput query="inactivePages">

#inactivePages.ID# #inactivePages.Name# <br/>

</cfoutput>

<cfloop query="#inactivePages#">

#inactivePages.ID# #inactivePages.Name# <br/>

</cfloop>

<cfscript>

for (row in inactivePages) {

writeOutput(row.id);

...

}

</cfscript>

-Nic

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 05, 2017 1
Engaged ,
Dec 06, 2017

Copy link to clipboard

Copied

Thank you so much for the help.

he following example cause this error:

Exception: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

Message: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

<cfscript>

for (row in inactivePages) {

writeOutput(row.id);

...

}

</cfscript>


And the following code example show this error:

Message: Attribute validation error for tag cfloop.

Type: Application

Detail: The value of the attribute query, which is currently <Reports_GetInactivePages_Result><LimitExceeded type="bool">0</LimitExceeded><resultquery class="array"><item><Date>2016-09-15 14:47:32</Date><Description>About</Description><FileName>index.cfm</FileName><ID type="int">138299</ID><IconPath>/commonspot/dashboard/images/type_icons/pgtype_0.png</IconPath><IconText>CommonSpot Page</IconText><IsUploadedDoc type="bool">0</IsUploadedDoc><Link>/training/01/mathematics/about/index.cfm</Link><LockUserID type="int">0</LockUserID><MyApprovalNeeded type="bool">0</MyApprovalNeeded><MyWIP type="bool">0</MyWIP><OthersApprovalNeeded type="bool">0</OthersApprovalNeeded><OthersWIP type="bool">0</OthersWIP><OwnerID type="int">1001171</OwnerID><OwnerName>SAMPLEML</OwnerName><PageActivationStatus>1</PageActivationStatus><PageType>0</PageType><Scheduled type="bool">0</Scheduled><SubsiteURL>/training/01/mathematics/about/</SubsiteURL><Title>About</Title></item><item><Date>2016-10-06 09:42:15</Date><Description>About</Description><FileName>index.cfm</FileName><ID type="int">139809</I..., is invalid.

<cfloop query="#inactivePages#">

ID: #inactivePages.id<br />

Title: #inactivePages.title# <br/>

Subsite: #inactivePages.subsiteurl#<br />

</cfloop>

And this code example below shows this error:

Message: Attribute validation error for tag cfoutput.

Type: Application

Detail: The value of the attribute query, which is currently inactivePages, is invalid.

<cfoutput query="inactivePages">

#inactivePages.ID# #inactivePages.Name# <br/>

</cfoutput>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 06, 2017 0
Engaged ,
Dec 06, 2017

Copy link to clipboard

Copied

By the way, name, title, id, and so on are not rows. They are column headers.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 06, 2017 0
Engaged ,
Dec 06, 2017

Copy link to clipboard

Copied

Here's how I got it work.

<cfloop query="#inactivePages.ResultQuery#">

     <cfoutput >

          Title:  #subsiteurl#<br />

     </cfoutput>

</cfloop>

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 06, 2017 0
Adobe Community Professional ,
Dec 06, 2017

Copy link to clipboard

Copied

So it looks like the challenge (which stumped you, and wasn't obvious to Nic and perhaps other readers) was that the result of the call to your getInactivePages method was not a query, but rather a struct that CONTAINED a query as one of its items. So Nic was close, as he thought inactivePages was a query.

As your code showed, you needed to refer not just to that, but to the query that was WITHIN it (resultquery). One can see on close examination that this was reflected in the dump you offered, but it was just as easy to miss that and think it was a dump of only a query. Glad you got it sorted. I have marked your last answer as the "correct" one.

/Charlie (server troubleshooter, carehart.org)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 06, 2017 0
Engaged ,
Dec 06, 2017

Copy link to clipboard

Copied

Exactly! That was my problem. I've searched on Google for looping through a struct but I have a query. So I've search for looping for query but I have a struct that contains a query.

Many thanks for this community's help!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 06, 2017 0
Adobe Community Professional ,
Dec 06, 2017

Copy link to clipboard

Copied

Well, I think what happened is that while you mentioned “struct” in your thread title, you did not mention it in any of your messages. Nic was showing how to loop through a query, because on first glance that looked like what you had and what you were asking about. Subtle difference.

It’s also why your google searching didn’t help. Even if you had found code for how to loop through the struct, when it got to the query within it, you would then have needed code to loop through it. And that’s where nic’s variations would have worked, at least if you pointed to the query within the struct.

Anyway, glad you got it sorted.

/charlie

/Charlie (server troubleshooter, carehart.org)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 06, 2017 0