输出产生不需要的空白

发布于 2024-10-20 04:08:15 字数 1635 浏览 6 评论 0原文

我正在输出类别,并在下面列出了子类别。如果每个子类别不是循环中的第一项,则会在其前面添加一个逗号。

另外,我只显示四个结果,因此如果记录计数超过四个,我需要将 ... 附加到第四个循环结果的末尾。

问题是,在应用 ... 的情况下,每个子类别后面都会有一个额外的空格。见下文: 在此处输入图像描述

看看逗号前面有一个空格吗?

代码:

<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'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:
enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-10-27 04:08:15

使用 listAppend 函数构建字符串:

<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... --->

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... --->
Saygoodbye 2024-10-27 04:08:15

在顶部使用 在底部使用 。然后使用 显式定义应输出到浏览器的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文