开发者

Coldfusion - Clearing & Appending Arrays

开发者 https://www.devze.com 2023-04-11 23:36 出处:网络
I asked a question here开发者_如何转开发 yesterday (cfquery grouping, counts & maxrows) to which this is related.That question would successfully answered in as much as the count aspect is working

I asked a question here开发者_如何转开发 yesterday (cfquery grouping, counts & maxrows) to which this is related. That question would successfully answered in as much as the count aspect is working successfully. However, I'm getting strange results with another aspect that I can quite figure out.

The scenario is this - I'm returning top-level categories and subcategories in a single query, the subcategories being returned by nesting cfoutput. The result works, with one exception - the subcategories are always returning the full list - i.e if I limit the initial output to return only 2 rows on screen, I'm always getting the first 2 rows from the first top-level category - now the first 2 rows from the 'current' top level category.

I've tried clearing the array within the cfoutput query, within the cfloop etc - all without success.

I've pasted my code below, and attached a screenshot of the current out it generates. Any pointers on this before I lose all my hair would be gratefully accepted!

<cfquery name="getcategories">
SELECT p.ID AS CategoryID, p.Cat_Name as CategoryName, p.Cat_Shortname, c.ID AS SubCategoryID, c.Cat_Name as SubCategoryName, c.Cat_Shortname AS SubCatShortname
FROM    product_categories p LEFT JOIN product_categories c ON p.ID = c.SubcategoryOf
WHERE  p.SubcategoryOf = 0
</cfquery> 
<cfset subcategoryNames = ArrayNew(1)>
        <cfoutput query="getcategories" group="CategoryName">
        <li class="catli">CategoryName : #CategoryName#</li> 
        <cfoutput>

          <cfset arrayAppend(subcategoryNames, SubcategoryName)>
          <p>ArrayAppend : #SubcategoryName#<br /></p>
        </cfoutput>

        <cfloop from="1" to="2" index="i">
            <li class="subli">SubcategoryName : #SubcategoryName[i]#</li>
        </cfloop>

        <cfif arrayLen(subcategoryNames) GT 2>
        <p>ArrayLen is GT 2</p>
          <li class="subli moreli">
            + #arrayLen(subcategoryNames) - 2# More Subcategories
          </li>
            <!--- Extra Subcategories --->
            <p>Extra Subcategories</p>
                <cfloop from="3" to="#arrayLen(subcategoryNames)#" index="r">
                <li class="subli">SubcategoryName : #SubcategoryName[r]#</li>
                </cfloop>

            <!--- End Extra Subcategories --->

        </cfif>

        </cfoutput>

Coldfusion - Clearing & Appending Arrays


You should just be able to switch these two lines:

<cfset subcategoryNames = ArrayNew(1)>
<cfoutput query="getcategories" group="CategoryName">

To this:

<cfoutput query="getcategories" group="CategoryName">
    <cfset subcategoryNames = ArrayNew(1)>

That will reset your array at the beginning of each new top-level category.

You could also leave them in their original order, and just do this:

<cfset subcategoryNames = ArrayNew(1)>
<cfoutput query="getcategories" group="CategoryName">
    <cfset ArrayClear(subcategoryNames) />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号