Solved

coldfusion conditional formatting if text string contains "buzzwords"

Posted on 2010-09-15
11
1,254 Views
Last Modified: 2013-12-24
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!
0
Comment
Question by:Bang-O-Matic
  • 4
  • 4
  • 3
11 Comments
 
LVL 3

Expert Comment

by:Gjslick
ID: 33686733
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

0
 

Author Comment

by:Bang-O-Matic
ID: 33688130
Thanks for the help! the code ran without error but didn't bold or capitalize any words.
0
 
LVL 3

Expert Comment

by:Gjslick
ID: 33688150
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?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33690494
(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() ..

0
 

Author Comment

by:Bang-O-Matic
ID: 33692513
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

0
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 
LVL 52

Expert Comment

by:_agx_
ID: 33694891
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>
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 50 total points
ID: 33694953
>> <cfoutput query="q">
>>      <cfset col101 = "I was changed">


Apparently when you modify the value inside the output loop, CF adds the value to the VARIABLES scope.  It doesn't modify the query value.  So if you wanted to display the new value, you'd have to specify the scope explicitly

<cfset q = queryNew("")>
<cfset queryAddColumn(q, "col101", listToArray("Original Value"))>
<cfoutput query="q">
      <cfset col101 = "I was changed">
      #VARIABLES.col101#   <!---- specify the scope ---->
</cfoutput>

... OR specify the query explicitly when you modify the value.

<cfset q = queryNew("")>
<cfset queryAddColumn(q, "col101", listToArray("Original Value"))>
<cfoutput query="q">
      <cfset q.col101[currentRow] = "I was changed">
      #col101#
</cfoutput>


Personally, I'd just use a different variable name instead ;-)

0
 

Author Comment

by:Bang-O-Matic
ID: 33695176
<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?

0
 
LVL 3

Accepted Solution

by:
Gjslick earned 450 total points
ID: 33696013
Ah, I see, I didn't realize that you were outputting this in a query loop.  The problem you were having was actually a scoping issue, where even though it looked like you replaced a variable with the same name, a new variable was actually being created and put into a different scope (the 'variables' scope).  The 'query' scope is at the top of the search chain when a variable's value is requested, but when you use , the variable that you set is automatically put into the 'variables' scope (which is lower on the variable name search chain).

So basically you had two separate variables:
'query.col101' (the original string) and,
'variables.col101' (the string with the bold replacements)

So 'variables.col101' was holding the replacements with the bold text, but when you went to output #col101# later, it was really outputting #[query].col101# (which had none of the replacements).

--------------------------------------

With replace(), I did actually want to use the regular replace() function instead of replaceNoCase(), because I didn't want to replace say, "Quartz" with "quartz" by accident.  That's why there are two replace() function calls: one to replace all lower case occurrences with the lowercase equivalent, and the other to replace any occurrences that had a capital first letter with the appropriate capped-first-letter transformation of the buzz word.  Btw, just to note, put your buzz words in the list as all lowercase and it should work correctly.

--------------------------------------

The way that you have the code in the latest post with using a new variable for the replacements is the reason why only the last buzz word is getting bolded.  If you follow the code, you will see that you are actually replacing the first buzz word on the first loop iteration, but then the code overwrites that first result when the second loop iteration executes, therefore only giving you the replacements for the second (i.e. last) buzz word in the list.  The idea of a separate variable was a good one though, but you just needed to initialize it above the loop. The solution should be the code snippet below.


Thanks for responding and giving your input on this btw _agx_, you deserve some of the points.

-Greg
<cfset boldedCol101 = col101>

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

<cfoutput>#boldedCol101#</cfoutput>

Open in new window

0
 

Author Comment

by:Bang-O-Matic
ID: 33696793
Thanks Gjslick and agx, it worked perfectly!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33697595
Cool.  Glad to hear it, and nice job Gjslick  :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now