Solved

Highlight a word from some text

Posted on 2016-08-01
16
64 Views
Last Modified: 2016-08-01
I have a page that displays text from a nvarchar (8000) field in my database as: <%=(report_results.Fields.Item("comments").Value)%>

I am also passing a 'keyword' from the previous page and I retrieve it with: request.form("keyword")  

I want to highlight the value I pass as keyword in the text that is displayed on the page.  Example:

I my keyword is "second"

I want the text to display as "This is the second time I try this"  or something of that nature. How can I do this ?

I am using SQL 2008 and ASP Classic
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738324
what you see in your page is generated and displayed as HTML. Hence, to highlight certain words based on certain actions, you need to change the HTML codes as well.

so, now the question is what is the container of that area to display your wordings? is it a DIV, etc? do you have the codes to post here?
0
 

Author Comment

by:amucinobluedot
ID: 41738332
Sure. This is the whole table where I have a repeated region showing the results.

 <table class="table table-striped table-bordered table-hover dataTables-example" >
                <thead>
                  <tr>
                    <th width="3%">Actions</th>
                    <th width="10%">User</th>
                    <th width="46%">Notes</th>
                    <th width="10%">Case number</th>
                    <th width="13%">Contact</th>
                    <th width="8%">Last updated</th>
                    <th width="10%">Case status</th>
                    </tr>
                </thead>
                <tbody>
                <% 
While ((Repeat1__numRows <> 0) AND (NOT report_results.EOF)) 
%>
                    <tr class="gradeX">
                      <td><a href="../cases/notes_index.asp?caseId=<%=(report_results.Fields.Item("caseid").Value)%>" target="_blank" class="btn btn-white btn-sm"><i class="fa fa-folder" text-navy></i></a></td>
                      <td><%=(report_results.Fields.Item("atty").Value)%></td>
                      <td><%=(report_results.Fields.Item("comments").Value)%></td>
                      <td><%=(report_results.Fields.Item("caseid").Value)%></td>
                      <td><%=(report_results.Fields.Item("Alien").Value)%></td>
                      <td><%= DoDateTime((report_results.Fields.Item("Lastupdate").Value), 2, 9225) %></td>
                      <td><%=(report_results.Fields.Item("casestatus").Value)%></td>
                    </tr>
                                    <% 
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  report_results.MoveNext()
Wend
%>
                </tbody>
                <tfoot>
                  <tr>
               <th width="3%">Actions</th>
                    <th width="10%">User</th>
                    <th width="46%">Notes</th>
                    <th width="10%">Case number</th>
                    <th width="13%">Contact</th>
                    <th width="8%">Last updated</th>
                    <th width="10%">Case status</th>
                    </tr>
                    

                </tfoot>
              </table>

Open in new window


The field that will include the text that should highlight any matching keywords is: <%=(report_results.Fields.Item("comments").Value)%>

Let me know if you need anything else.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738335
is that the content of report_results.Fields.Item("comments").Value is plain text or already in HTML?
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:amucinobluedot
ID: 41738337
It is HTML code. User saves the content from an HTML editor, this is an example:

<p><strong>Client is traveling to:</strong> <span style="color:#ff0000">This is an example of a note</span></p><p>From:<br />To:</p><p>Additional comments:&nbsp;</p>

Open in new window


This is another example where user doesn't do anything other than enter text in the editor

test note no styling

Open in new window

0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41738367
you probably can refer to this article:

Keyword highlighting in ASP
http://www.codeproject.com/Articles/610/Keyword-highlighting-in-ASP

then try something like:
<%=Highlight(report_results.Fields.Item("comments").Value, "second"  , "<font color=red><b>", "</b></font>"  )%>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41738400
I made a mistake and closed the question. Unfortunately it didn't work, it doesnt highlight the word  :$
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738402
let me try and get back to you shortly..
0
 

Author Comment

by:amucinobluedot
ID: 41738404
Sounds good. Same result with other browsers
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738407
simple test looks good:
<%


text = "<p><strong>Client is traveling to:</strong> <span style=""color:#ff0000"">This is an example of a note</span></p><p>From:<br />To:</p><p>Additional comments:&nbsp; whatever this is second ++ </p>"

response.write Highlight(text, "second"  , "<font color=red><b>", "</b></font>"  )

Function Highlight(strText, strFind, strBefore, strAfter)
	Dim nPos
	Dim nLen
	Dim nLenAll
	
	nLen = Len(strFind)
	nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1

	Highlight = strText

	If nLen > 0 And Len(Highlight) > 0 Then
		nPos = InStr(1, Highlight, strFind, 1)
		Do While nPos > 0
			Highlight = Left(Highlight, nPos - 1) & _
				strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
				Mid(Highlight, nPos + nLen)

			nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
		Loop
	End If
End Function

%>

Open in new window


>>Ok. For some reason it only highlited in red the first time I did it, then the results page keeps highlighting that one word.
Do you have a screenshot to share here?
0
 

Author Comment

by:amucinobluedot
ID: 41738416
Well I think the problem is that I copy/pasted the code. It is always using 'second' to highlight, I should change it to the value of the form

<%=Highlight(report_results.Fields.Item("comments").Value, "second"  , "<font color=red><b>", "</b></font>"  )%>

Open in new window


How can I replace 'second'  for <%=request.form("keyword")%>   ?
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738419
alright, I think you got the problem resolved.
0
 

Author Comment

by:amucinobluedot
ID: 41738421
Not 100% ... just need a little help with the syntax to use the form value instead of the hard coded value of 'second'
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738429
alright, so try this:
<%=Highlight(report_results.Fields.Item("comments").Value, request.form("keyword")  , "<font color=red><b>", "</b></font>"  )%>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41738433
Awesome !  thanks so much. all set here.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 41738434
coool, glad that it works!
0
 

Author Comment

by:amucinobluedot
ID: 41738435
It does, perfectly. Have a good night!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to dynamically set the form action using jQuery.

730 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