Link to home
Start Free TrialLog in
Avatar of Dusty
DustyFlag for United States of America

asked on

coldfusion conditional formatting if text string contains "buzzwords"

In one column, "remarks",  of my real estate database is a varchar field that contains comments about each property. What I want to do is create a list of "buzzwords" like Granite, Quartz, Pool, etc... and if the remarks text for that property contain these words I want them to be bolded when output on my page.

 The code below bolds the complete remarks field, how can I just bold the buzzwords?

<cfif #remarks# CONTAINS "Quartz" OR "Granite"><b>#remarks#</b><cfelse>#remarks#</cfif>

Thanks!
Avatar of Gjslick
Gjslick
Flag of United States of America image

This might be a bit of a simplistic solution, but should work.  
<cfset buzzWords = "quartz,granite">
<cfloop list="#buzzWords#" index="buzzWord">
	<!--- Replace occurrences of the current buzz word with all lowercase letters --->
	<cfset remarks = replace( remarks, buzzWord, "<b>#buzzWord#</b>", "all" )>
	
	<!--- Replace occurrences of the current buzz word where the word is at the 
	      beginning of a sentence, where its first letter is capitalized. --->
	<cfset cappedBuzzWord = uCase( left( buzzWord, 1 ) ) & right( buzzWord, len( buzzWord ) - 1 )>
	<cfset remarks = replace( remarks, cappedBuzzWord, "<b>#cappedBuzzWord#</b>", "all" )>
</cfloop>

<cfoutput>#remarks#</cfoutput>

Open in new window

Avatar of Dusty

ASKER

Thanks for the help! the code ran without error but didn't bold or capitalize any words.
Hmm, worked fine on my end.  Can you post what you have of your code, and what the variable 'remarks' would have stored in it before running the replacement code on it?
Avatar of _agx_
(no points please...)

>> replace( remarks,

I haven't tested the code but could it be a case-sensitivity issue?  replace() is case sensitive. Try it with replaceNoCase() ..

Avatar of Dusty

ASKER

AGX I tried replaceNoCase() and still didn't work for me.

GJslick here is the detail.cfm page code below and an example url that has granite and quartz in the remarks.
http://68.101.50.2/cbsskwr/details.cfm?col1=93328

Please note that the field in my table is actually "col101" not "remarks" .

Here is the text copied directly from the table field for the property in the above url.

What more could you possibly want?This home has EVERYTHING plus it's located in prestigious  Eagle Springs.This home offers a Formal DR, Keeping room w/ fireplace, LR w/ fireplace, beautifully hand scraped hardwood floors,Open kitchen w/ stainless appliances,Quartz granite counter tops,tile in wet areas,Master bath has jetted tub,oversized limestone shower w/ large double shower heads, granite countertops in ALL bathrooms,oversized garage w/ door entry,large covered patio, privacy fence, full sod/sprinkler.

<cfset variables.IDnumber = "#URL.col1#">
<cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
<cfif Not structkeyexists(cookie,"favpage")>
  <cfset cookie.favpage="">
</cfif>
<cfquery name="details" datasource="CGMLS">
SELECT * FROM cgmls.wrlistings WHERE col1=#URL.col1#
</cfquery>
<cfoutput query="details">


 <cfset buzzWords = " granite,quartz">
<cfloop list="#buzzWords#" index="buzzWord">
	<!--- Replace occurrences of the current buzz word with all lowercase letters --->
	<cfset col101 = replace( col101, buzzWord, "<b>#buzzWord#</b>", "all" )>
	
	<!--- Replace occurrences of the current buzz word where the word is at the 
	      beginning of a sentence, where its first letter is capitalized. --->
	<cfset cappedBuzzWord = uCase( left( buzzWord, 1 ) ) & right( buzzWord, len( buzzWord ) - 1 )>
	<cfset col101 = replace( col101, cappedBuzzWord, "<b>#cappedBuzzWord#</b>", "all" )>
</cfloop>  

  
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html>
  <head>
  <title></title>
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link href="cbstyle.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/easySlider1.5.js"></script>
  <script type="text/javascript" src="js/slider.js"></script>
  <script type="text/javascript" src="js/bslider.js"></script>
  <link href="slider.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
  <div id="site">
    <cfinclude template="header.cfm">
    <!---left main div--->
    <div id="main3_left">
      <h1>#col6# #col8# &nbsp;#col10#, GA #col12#</h1>
      <P>&nbsp;</P>
      <h3>#RemoveChars(DollarFormat(col5),Len(DollarFormat(col5))-3,3)# | SQFT: #col68# | Status: #col13# | Subdivision: #col58#</h3>
      </br>
      <p>&nbsp;</p>
      <div id="photos">
        <div id="left">
          <div id="object1">
            <!---mainpic--->
            <cfdirectory action="list" directory="C:\inetpub\idx\ftp\pics_wr\images\" filter="#variables.IDnumber#*.jpg" name="files">
            <cfif files.recordCount eq 0>
              <img src="images/nophoto.jpg" alt="no photo"  class="alignleft" id = "picmain" />
              <cfelse>
              <p> <img src="http://68.101.50.2/ftp/pics_wr/images/#col1#.jpg" height="240" width="320" alt=""  class="left" id = "picmain" /></p>
            </cfif>
          </div>
        </div>
        <div id="right">
          <div id="object3">
            <!---thumbs--->
            <cfdirectory action="list" directory="C:\inetpub\idx\ftp\pics_wr\images\" filter="#variables.IDnumber#*.jpg" name="files">
            <cfif files.recordCount eq 0>
              <cfelse>
              <cfloop query="files">
                <a href="javascript:;" onmouseover="picmain.src='http://68.101.50.2/ftp/pics_wr/images/#files.name#';"><img src="http://68.101.50.2/ftp/pics_wr/images/#files.name#" width="47" height="40" alt="" /></a>
              </cfloop>
            </cfif>
          </div>
        </div>
      </div>
      <p>&nbsp;</p>
      <p class="subheadingMedBlue">Remarks:</p>      
      
     <p>#col101#</p>     
      
      
       <p>&nbsp;</p>
       <hr class="line" />
       <p>&nbsp;</p>
      <p>MLS Number: #col1#</p>
      <p>Type: #col3#</p>
      <p>Status: #col13#</p>
      <p>Number of Living Areas: #col16#</p>
      <p>Number of Bedrooms: #col17#</p>
      <p>Number of Full Baths: #col18#</p>
      <p>Number of Half Baths: #col23#</p>
      <p>Master Bedroom on Main Floor?: #col20#</p>
      <p>Garage: #col19#</p>
      <p>Split bedroom Plan?: #col22#</p>
    </div>
    <!---right column main div--->
    <div id="main3_right"></br>
      <div>
        <p class="welc1">Features:</p>
        <hr class="line" />
        <p>&nbsp;</p>
        <div id="features">
          <div id="left">
            <div id="object1"><img src="images/icon_beds.gif" alt="" width="45" height="45" align="middle" class="alignleft" /> <span class="smallorangetext">#col17#</span></div>
          </div>
          <div id="right">
            <div id="object3"><img src="images/icon_baths.gif" alt="" width="45" height="45" align="middle" class="alignleft" /><span class="smallorangetext">#col18#</span></div>
          </div>
        </div>
        <hr class="line" />
        <p>&nbsp;</p>
        <div id="features">
          <div id="left">
            <div id="object1"><img src="images/icon_sqft.gif" alt="" width="45" height="45" align="middle" class="alignleft" /> <span class="smallorangetext">#col68#</span></div>
          </div>
          <div id="right">
            <div id="object3"><img src="images/icon_garage.gif" alt="" width="45" height="45" align="middle" class="alignleft" /> <span class="smallorangetext">#col19#</span></div>
          </div>
        </div>
        <hr class="line" />
        <p>&nbsp;</p>
        <div>
        <a href="addfavorites.cfm?favID=#col1#|#trim(col6)# #trim(col8)#" target="hide">
        <cfif listfindnocase(cookie.favpage,"#col1#|#trim(col6)# #trim(col8)#")><img src="images/favs-lg.jpg" alt="" /><br />Remove From Favorites
		<cfelse>
        <img src="images/fav.gif" alt="" align="middle" />Add To Favorites</cfif></a>
        <p>&nbsp;</p></div>
        <iframe name="hide" id="hide" style="height:0px;width:0px;"></iframe>
        <div>
          <p>
          <div></div>
          </p>
        </div>
        <p>&nbsp;</p>
      
      </div>
    </div>
    <br />
    <div id="spacer"></div>
  <cfinclude template="footer.cfm">
  </body>
  </html>
</cfoutput>

Open in new window

Wait a sec....  (I still haven't tested the code). But it's entirely possible it IS working. But CF isn't actually using your new value.  It's displaying the old one.  Try using a different variable name when you do the replace

ie
<cfset newCol101Value = replace( col101, buzzWord, "<b>#buzzWord#</b>", "all" )>
....
<!--- later on display the new value --->
#newCol101Value#


Unfortunately it's one of those gotcha's that happens when using <cfoutput query="..">. Here's an example to demonstrate. You'd expect to see "I was changed". But instead you see "Original Value"


<cfset q = queryNew("")>
<cfset queryAddColumn(q, "col101", listToArray("Original Value"))>
<cfoutput query="q">
      <cfset col101 = "I was changed">
      #col101#
</cfoutput>
SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dusty

ASKER

<cfset buzzWords = "granite,quartz">
<cfloop list="#buzzWords#" index="buzzWord">
      <!--- Replace occurrences of the current buzz word with all lowercase letters --->
      <cfset newCol101Value = replace( col101, buzzWord, "<b>#buzzWord#</b>", "all" )>
      
      <!--- Replace occurrences of the current buzz word where the word is at the
            beginning of a sentence, where its first letter is capitalized. --->
      <cfset cappedBuzzWord = uCase( left( buzzWord, 1 ) ) & right( buzzWord, len( buzzWord ) - 1 )>
      <cfset newCol101Value = replace( col101, buzzWord, "<b>#buzzWord#</b>", "all" )>
</cfloop>  


AGX I tried what you suggested and got mixed results:

 <cfset buzzWords = "granite,quartz"> neither word bolded.
 <cfset buzzWords = "granite,Quartz"> Quartz is bolded but granite is not.
 <cfset buzzWords = "Quartz">works, bolded.
 <cfset buzzWords = "quartz">not bolded
 <cfset buzzWords = "granite"> works, bolded
 <cfset buzzWords = "quartz,granite">granite is bolded but quartz is not.
 <cfset buzzWords = "Quartz,granite">granite is bolded but Quartz is not.

it seems that it's picking up the last buzzword in the list and skipping over the others?

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dusty

ASKER

Thanks Gjslick and agx, it worked perfectly!
Cool.  Glad to hear it, and nice job Gjslick  :)