Solved

Highlight a word from some text

Posted on 2016-08-01
16
40 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
  • 8
  • 8
16 Comments
 
LVL 49

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 49

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
 

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 49

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 49

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 49

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 49

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 49

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 49

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 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

18 Experts available now in Live!

Get 1:1 Help Now