Skip navigation
Currently Being Moderated

pass id into custom tag

Jan 16, 2013 2:13 PM

<!---page1.cfm--->

query returned 3 IDs (1,2 and 3).  Loops over each ID and call page2 then pass each ID over. 

 

<cfloop query="my_qr">

<cf_page2 id= "#id#" />

</cfloop>

 

<!---page2.cfm--->

<cfoutput>

ID:#attributes.id#

</cfoutput>

 

tested it and only frst ID (1) is passed over to page2 instead of 1, 2 and 3? 

 

thanks

 
Replies
  • Currently Being Moderated
    Jan 16, 2013 7:28 PM   in reply to newcf

    Is the first attribute being passed once or thrice?  What happens if you do this?

     

    <cfloop query="my_qr">

    <cfdump var="before #id#> <br>

    <cf_page2 id= "#id#" />

    <cfdump var="after #id#><br>

    </cfloop>

     

    <!---page2.cfm--->

    <cfoutput>

    ID:#attributes.id#

    </cfoutput>

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 17, 2013 2:23 AM   in reply to newcf

    Firstly, you should always scope your query variables.  This helps make the code more readable (so you always know where a value is coming from), and more importantly, prevents the chances of for instance 'id' having been defined in a different scope, which your code is then using instead of the 'id' from the query you expect it to use.

     

    <cfloop query="my_qr">

    <cf_page2 id= "#my_qr.id#" />

    </cfloop>

     

    Secondly, you have a closing /> on your custom tag call to page2.cfm.  This is the equivalent of doing <cf_page2></cf_page2>.  Which means page2.cfm gets executed twice, once for the opening tag and once for the closing tag.  Normally you'd check the thisTag.executionMode to see which part it's being called from.  See http://www.petefreitag.com/item/64.cfm  - you probably just want to remove the trailing / from <cf_page2 id= "#my_qr.id#" /> , so it only gets executed once (and you don't have to then rewrite page2.cfm).

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 17, 2013 9:15 AM   in reply to newcf

    If you got the same id twice, try changing to this

         id= "#my_qr.id#"

    to this

         id= "#my_qr.id[current_row]#"

     

    If you don't specify the row, ColdFusion uses the first one.  That's not normally necessary in a query loop, but it might be in this case because you qualified the variable.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 20, 2013 5:10 AM   in reply to newcf

    I am with Duncancumming. It is as simple as

     

    <cfloop query="my_qr">

    <cf_page2 id="#id#">

    </cfloop>

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 20, 2013 5:28 AM   in reply to newcf

    <!---page1.cfm--->

    query returned 3 IDs (1,2 and 3).  Loops over each ID and call page2 then pass each ID over. 

     

    <cfloop query="my_qr">

    <cf_page2 id= "#id#" />

    </cfloop>

     

    <!---page2.cfm--->

    <cfoutput>

    ID:#attributes.id#

    </cfoutput>

     

    tested it and only frst ID (1) is passed over to page2 instead of 1, 2 and 3? 

     

    Is it a case of onlythe first ID is being passed (so you get "1","1","1"), or that the loop is not iterating three times (eg: you're getting just "1")?

     

    Irrespective of whether you scope your query, the col in the query should be the first one one found when CF goes to find what you mean by "id", so even if you had URL.id, form.id and variables.id set elsewhere, if my_qr has an "id" column, that'd be the one used. This code demonstrates:

     

    <!--- test.cfm --->

    <cfscript>

              URL.id = 4;

              form.id = 5;

              variables.id = 6;

     

              q = queryNew("");

              queryAddColumn(q, "id", [1,2,3]);

              queryAddColumn(q, "data", ["one", "two", "three"]);

    </cfscript>

     

     

    <cfloop query="q">

              <cf_tag id="#id#">

    </cfloop>

     

     

    <!--- tag.cfm --->

    <cfparam name="attributes.id" default="0">

    <cfoutput>

    ID:#attributes.id#<br />

    </cfoutput>

     

    This outputs:

    ID:1

    ID:2

    ID:3

     

    Which is what one should expect.

     

    I ran this test on CF9.0.1, but woudl expect CFMX7.x to be the same. I will verify this when I get home in a coupla hours time (I don't have CFMX7.x to hand here).

     

    --
    Adam

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points