Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop question...

Status
Not open for further replies.

net123

Programmer
Oct 18, 2002
167
US

I'm having trouble with a loop:

This is what I currently have:

<CFIF IsDefined('qry1')>
<CFLOOP QUERY = &quot;qry1&quot;>
<CFHTMLHEAD TEXT = &quot;
<META NAME = 'description' CONTENT = '#qry1.content#'>
<META NAME = 'keywords' CONTENT = '#qry1.content#,'>
&quot;>
</CFLOOP>
</CFIF>
[/red]

and of course, the output looks something like this:


<META NAME = 'description' CONTENT = 'content_1'>
<META NAME = 'keywords' CONTENT = 'content_1'>

<META NAME = 'description' CONTENT = 'content_2'>
<META NAME = 'keywords' CONTENT = 'content_2'>

.
.
.

<META NAME = 'description' CONTENT = 'content_n'>
<META NAME = 'keywords' CONTENT = 'content_n'>
[/red]

But the output I am looking for is as follows:

<META NAME = 'description' CONTENT='content1, content_2, ..., content_n'>
<META NAME = 'keywords' CONTENT='content1,content_2, ..., content_n'>
[/red]

The answer I know must be simple, but I can't seem to grasp it. Somehow, I can't figure out how to get the loop within the CONTENT part of the code.

 
<CFIF IsDefined('qry1')>

<CFHTMLHEAD TEXT = &quot;
<META NAME = 'description' CONTENT = <CFLOOP QUERY = &quot;qry1&quot;>'#qry1.content#'</CFLOOP>>
<META NAME = 'keywords' CONTENT = <CFLOOP QUERY = &quot;qry1&quot;>'#qry1.content#,'</CFLOOP>>
&quot;>

</CFIF>
DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
DeZiner:

Your solution is one of the ones I had already tried, but if you will notice, one cannot use multiple double quotes within the outside double quotes of the CFHTMLHEAD tag. ColdFusion will not be able to parse your code properly and thus give you an error. And if one puts single quotes around the the actual 'queries', the following is the output which is not what am I looking for:

<META NAME = 'description' CONTENT = <CFLOOP QUERY = 'qry1'>'content'</CFLOOP>>
<META NAME = 'keywords' CONTENT = <CFLOOP QUERY = 'qry1'>'content,'</CFLOOP>>

Anyone else have any ideas?

 
Instead of:
Code:
<CFIF IsDefined('qry1')>
 <CFLOOP QUERY = &quot;qry1&quot;>
  <CFHTMLHEAD TEXT = &quot;
   <META NAME = 'description' CONTENT = '#qry1.content#'>
   <META NAME = 'keywords' CONTENT = '#qry1.content#,'>
  &quot;>
 </CFLOOP>
</CFIF>

Try something like this:
Code:
<cfset contents = &quot;&quot;>
<cfset descr=&quot;&quot;>
<CFIF IsDefined('qry1')>
 <CFLOOP QUERY = &quot;qry1&quot;>
  <CFSET contents = contents & ' ' & qry1.content>
  <CFSET descr = descr & ' ' & qry1.content>
 </cfloop>
 <CFHTMLHEAD TEXT = &quot;
  <META NAME = 'description' CONTENT = '#contents#'>
  <META NAME = 'keywords' CONTENT = '#descr#'>
  &quot;>
 </CFLOOP>
</CFIF>

You'd have to do a little extra work to get the commas, prevent the leading space, etc, but this should get you started in the right direction.

Good luck!
 
The &quot;little extra work&quot; malleykr mentions can be as simple as using
Code:
ListAppend
, rather than
Code:
contents = contents & ' ' & qry1.content
:

Code:
<cfset contents = &quot;&quot;>
<CFIF IsDefined('qry1')>
 <CFLOOP QUERY = &quot;qry1&quot;>
  <!--- and since the content for the description and
        keywords are the same (???), you only need to
        stuff one list --->
  <CFSET contents = ListAppend(&quot;#contents#&quot;,qry1.content)>
 </CFLOOP>
 <CFHTMLHEAD TEXT = &quot;
  <META NAME='description' CONTENT='#contents#'>
  <META NAME='keywords' CONTENT='#contents#'>
  &quot;>
</CFIF>

The other thing you can do is use double double-quotes. This is ColdFusions way of escaping quotes.

Code:
<CFHTMLHEAD TEXT = &quot;
  <META NAME=&quot;&quot;description&quot;&quot; CONTENT=&quot;&quot;#contents#&quot;&quot;>
  <META NAME=&quot;&quot;keywords&quot;&quot; CONTENT=&quot;&quot;#contents#&quot;&quot;>
  &quot;>

That being said... is there a reason you're using CFHTMLHEAD, rather than simply inserting CFML directly into the head of the page?
Hope it helps,
-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top