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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word count issue 1

Status
Not open for further replies.

evergreean43

Technical User
May 25, 2006
165
US
I need to show max words of 30 in my output of search variable output similiar to an article teaser. The output should show like this:
here is the teaser which should show 30 words.....

Here is how I think I can start but not sure because I cant figure the spaces in my word count:
Code:
<cfset charcount = 300>  
<cfif Len(MyQuery.Article) gt charcount>
<cfset Variables.NewArticle = "#Left(MyQuery.Article, charcount)##ListGetAt(RemoveChars(MyQuery.Article, 1, charcount), 1, " ")#">
#Variables.NewArticle# ... < a href="filename.cfm?ArticleID=#MyQuery.ArticleID#"><more>
<cfelse>
#MyQuery.Article#
</cfif>

Please advise how I can do this?

 
are you actually going to be using the entire Article on the page, or only the teaser?

which database is this? because if it's mysql, you can select just the teaser real easy right in the sql

otherwise, you can bring in, say, the first 300 characters in the query, and then split it up from there...

r937.com | rudy.ca
 
To do it in CF is not that hard. You forget, as many do, that you can treat the string as a list with spaces as the delimeter.

One way of doing this is to use ListToArray and then loop over the first 30 array entries to get your first 30 words.

<cfset myArray = #ListToArray(MyQuery.Article)#>
<cfif myArray.len GTE 30>
<cfset teaser = "">
<cfloop from=0 to=30 index="i">
<cfset teaser = #ListAppend(teaser,myArray)#>
</cfloop>
<cfoutput>teaser</cfoutput>


DonOmite
 
Its an Access 2000 database and just want to use the teaser.

Thanks.

I tried this and it gave me error about array variable:
Code:
<cfset myArray = #ListToArray(MyQuery.Article)#>
<cfif myArray.len GTE 30>
<cfset teaser = "">
<cfloop from=0 to=30 index="i">
<cfset teaser = #ListAppend(teaser,myArray[i])#>
</cfloop>
<cfoutput>teaser</cfoutput>
</cfif>
 
Code:
<CFIF ListLen(MyQuery.Article,' ') LE 30 >
  <cfset teaser = MyQuery.Article >
<CFELSE>  
  <cfset teaser = '' > 
  <cfset temp = article >
  <CFLOOP from="1" to="30" index="i" >
    <cfset teaser = 
        ListAppend(teaser,ListFirst(temp,' '),' ') >
    <cfset temp = ListRest(temp,' ') >
  </CFLOOP>
</CFIF>
<p><CFOUTPUT>#teaser#</CFOUTPUT> </p>

r937.com | rudy.ca
 
I like the solution but really trying to understand how it works. If you have time can you advise if I have this correct?
I see you are using 3 functions in here that I am still trying to learn about called: ListAppend,ListFirst,ListRest

This ListAppend appends/changes the variable output where it works in the cfloop and adds the first word of the temp variable to a list and puts a space after the word? I assume after it does that the word is eliminated from the list (using the ListRest function) and it takes the first word available (which it gets from the ListFirst function) in the next iteration of the loop?
ListAppend(teaser,ListFirst(temp,' '),' ') >

Takes the temp variable with all the words and takes away the first word?
<cfset temp = ListRest(temp,' ') >



Code:
<CFIF ListLen(MyQuery.Article,' ') LE 30 >
  <cfset teaser = MyQuery.Article >
<CFELSE>  
  <cfset teaser = '' > 
  <cfset temp = article >
  <CFLOOP from="1" to="30" index="i" >
    <cfset teaser = 
        ListAppend(teaser,ListFirst(temp,' '),' ') >
    <cfset temp = ListRest(temp,' ') >
  </CFLOOP>
</CFIF>
<p><CFOUTPUT>#teaser#</CFOUTPUT> </p>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top