Reputation: 20004
I'm outputting categories with subcategories listed underneath. Each subcategory gets a comma prepended to it if it's not the first item in the loop.
Also, I am only displaying four results, so if the record count is more than four, I need to append ...
to the end of the fourth loop result.
The problem is that in instances where ...
has been applied there is an extra space after each subcategory. See below:
See how there is a space before the comma?
Code:
<ul class="defaultUL" style="float:right;">
<cfloop query="getParent" startrow="7" endrow="12">
<cfquery name="getSubCategory" datasource="dss">
SELECT Name, ID FROM Category WHERE ParentID = #getParent.ID#
</cfquery>
<cfset SubNumb = getSubCategory.recordcount>
<li><h3><a href="?Page=#Application.Utility.qsEncode(getParent.Name)#">#getParent.Name#</a></h3>
<cfloop query="getSubCategory" startrow="1" endrow="#SubNumb#">
<cfif SubNumb gt 4>
<cfif getSubCategory.currentRow lt 4 AND getSubCategory.currentRow gt 1>
, #getSubCategory.Name#
<cfelseif getSubCategory.currentRow eq 1>
#getSubCategory.Name#
<cfelseif getSubCategory.currentRow eq 4>
#getSubCategory.Name#...
</cfif>
<cfelse>
#getSubCategory.Name#,
</cfif>
</cfloop>
</li>
</cfloop>
</ul>
I made sure that the data in the database didn't have whitespace at the end.
Upvotes: 2
Views: 316
Reputation: 3781
Use the listAppend
function to construct your string:
<cfset subCatList = "" /> <!--- define a variable to hold the list of subcats; variable gets reset for each iteration of outer loop --->
<cfloop query="getSubCategory" startrow="1" endrow="4">
<!--- listAppend uses , as a the default delimiter. --->
<cfset subCatList = listAppend(subCatList, getSubCategory.Name) />
</cfloop>
<cfif getSubCategory.RecordCount gt 4>
<cfset subCatList = listAppend(subCatList,"...") />
</cfif>
<!---- value of subCatList at this point: subcat1,subcat2,subcat3,subcat4... --->
<!--- output subcatlist and fix spacing --->
#replace(subCatList, ",", ", ","all"#
<!--- output is subcat1, subcat2, subcat3, subcat4... --->
Upvotes: 7
Reputation: 3884
Use <cfsetting enablecfoutputonly="true" />
at the top and <cfsetting enablecfoutputonly="false" />
at the bottom. Then use <cfoutput></cfoutput>
to explicitly define what should be output to the browser.
Upvotes: 3