Forums

This topic is locked

Ordering RS by number of times keyword found

Posted 07 Nov 2002 21:16:07
1
has voted
07 Nov 2002 21:16:07 David Behan posted:
<b>Ordering RecordSet by number of times keyword found!!</b>

<b>Task:</b>
User enters keywords to be searched
Keyword passed into variable
Database queried using LIKE
Results displayed on page in the order of relevance (number of times keywords found)

<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>Example:
Records in Database - ordered by autonumber
Record 1: keyword found 26 times
Record 2: keyword found 44 times
Record 3: keyword found 30 times

Results on page - Ordered by number of times found
Record 2: keyword found 44 times
Record 3: keyword found 30 times
Record 1: keyword found 26 times
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>

So how do I do this? I was thinking of two ways:
1. Use ubound to count number of times found in recordset but how do I order it after going through database?
2. Use a combination of SELECT, COUNT and LIKE in the SQL statement but not sure if it is possible to go this route!!

Any suggestions would be appreciated...

Cheeurs,

Dave

_________________________
WinXP : IIS 5.1 : StudioMX : ASP : VBScript
www.clicksdesign.com

Replies

Replied 10 Nov 2002 18:38:25
10 Nov 2002 18:38:25 Dave Thomas replied:
I'd be interested in finding the solution to this also.

Regards,
Dave

UD4 | Flash 5 | SQL | Access | ASP/VBScript | XP-Pro
Replied 29 Nov 2002 10:49:29
29 Nov 2002 10:49:29 David Behan replied:
Ok,

After searching long and hard for an answer for this... I found nothing!!!! So, I am after writing a script to do it myself. This might not be the most practical way of doing it but IT WORKS!!! Maybe you could try it out on yours and give me your opinion on any errors or imporvements you suggest. Instructions below script.

HERE GOES:

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

<font color=red>
&lt;%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%&gt;
&lt;!--#include file="../sm_includes/config.asp" --&gt;
&lt;!--#include file="../sm_includes/functions.asp" --&gt;
&lt;!--#include file="../sm_includes/build.asp" --&gt;
&lt;%
'**************************************************************
'If keywords are empty, then redirect back to search page and
'display error message to the user
'**************************************************************
IF Request.Form("keywords" = "" AND Request.Form("search" = "go" THEN Response.Redirect("search.asp?error=nokw"

'**************************************************************
'Check if form was submitted and if so convert form variables
'to querystring variables and run page again.
'**************************************************************
IF Request.Form("search" = "go" THEN Response.Redirect("search.asp?search=go&keywords=" & Request.Form("keywords")


'**************************************************************
'Make sure we are actually to perform the search
'**************************************************************
IF Request.QueryString("search" = "go" THEN

'**************************************************************
'Collect the keywords from the form
'**************************************************************
keywords = Trim(Request.QueryString("keywords")

'**************************************************************
'Take the keywords and put them into an array
'**************************************************************
keywords2 = keywords
keywords2 = Replace(keywords2,"'","''"
keywords2 = Replace(keywords2,","," "
keywords2 = Replace(keywords2,";"," "
keywords2 = Replace(keywords2," "," "
keywords2 = Replace(keywords2," "," "
keywords2 = LCase(keywords2)
keywords2 = Trim(keywords2)

keywordarray = Split(keywords2," ",-1)

'**************************************************************
'Check which field to use for the KWC to be updated to the
'database using the application variable
'**************************************************************
current_KWC = Application("DWS_KWC"
IF current_KWC = "" THEN
new_KWC = 1
ELSE
IF current_KWC * 1 = 10 THEN
new_KWC = 1
ELSE
new_KWC = current_KWC + 1
END IF
END IF
Application("DWS_KWC" = new_KWC

'**************************************************************
'Get a full listing of the records in the database
'**************************************************************
sql_what = "*"
sql_table = "TBL_PAGES"
sql_where = "PAGE_ID &gt; 0"
sql_order = "PAGE_ID ASC"

CALL GetRecordSet(rsPagesKWC,cursor_type,sql_what,sql_table,sql_where,sql_order)

'**************************************************************
'Check each record in the database for the keywords entered and
'count the amount of times a keyword was entered. Then update
'the record with the total found in the next available field of
'keyword counts (KWC) based on the application variable.
'**************************************************************
DO WHILE NOT rsPagesKWC.EOF
keywordsfound = 0
keywords3 = split(keywords2," "
FOR EACH x IN keywords3
searchstart = 1
searchtext = rsPagesKWC("PAGE_FULL"
DO WHILE InStr(searchstart, LCase(searchtext), LCase(x)) &gt; 0
IF x = "" THEN EXIT DO
placefound = InStr(searchstart, LCase(searchtext), LCase(x))
searchstart = placefound + Len(x) + 1
keywordsfound = keywordsfound + 1
LOOP
NEXT

sql_table = "TBL_PAGES"
sql_set = "KWC_" & new_KWC & " = " & keywordsfound
sql_where = "PAGE_ID = " & rsPagesKWC("PAGE_ID"

CALL UpdateRecord(updKWC,sql_table,sql_set,sql_where)

rsPagesKWC.MoveNext
LOOP

'**************************************************************
'Close the recordset
'**************************************************************
CALL CloseRecordSet(rsPagesKWC)

'**************************************************************
'Get page details for all sub pages within this category
'**************************************************************

sql_what = "*"
sql_table = "TBL_PAGES"
sql_where = "KWC_" & new_KWC & " &gt; 0 AND PAGE_VIEWABLE = 'Y'"
sql_order = "KWC_" & new_KWC & " DESC"

CALL GetRecordSet(rsPageListings,cursor_type,sql_what,sql_table,sql_where,sql_order)

'Count number of records in recordset
sql_what = "COUNT(PAGE_ID) AS TOTAL"
sql_table = "TBL_PAGES"
sql_where = "KWC_" & new_KWC & " &gt; 0 AND PAGE_VIEWABLE = 'Y'"

CALL GetTotalSum(rsPageCount,cursor_type,sql_what,sql_table,sql_where)

'**************************************************************
'Listing Variables
'**************************************************************
IF NOT rsPageListings.EOF THEN top_KWC = rsPageListings("KWC_" & new_KWC)
date_format = 2057
record_count = rsPageCount("TOTAL"
record_offset = Request.QueryString("o"
listing_per_page = 5
IF record_offset = "" THEN record_offset = 1
IF record_offset = 0 THEN record_offset = 1

END IF

'**************************************************************
'Display error message if there is one
'**************************************************************
IF Request.QueryString("error" = "nokw" THEN
errortext = ("You did not enter any keywords to search on, please try again."
END IF

IF Request.QueryString("search" = "go" THEN
IF rsPageListings.EOF THEN
errortext = ("Your search did not yield any results, please try again."
END IF
END IF
%&gt;
&lt;%
design_to_use = "site"
Call BuildPageContent(design_to_use,ary_page_template)
Response.Write(ary_page_template(0))
%&gt;
&lt;table width="610" height="&lt;% = Application(ESM_LICENCE & "page_header_height" %&gt;" border="0" cellpadding="5" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td align="&lt;%= Application(ESM_LICENCE & "page_header_align" %&gt;" valign="bottom" class="pageheader"&gt;&lt;strong&gt;
Seach Site&lt;/strong&gt;&lt;/td&gt;
&lt;td width="100" align="&lt;%= Application(ESM_LICENCE & "page_header_align" %&gt;" class="pageheader"&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="600" border="0" cellspacing="0" cellpadding="5"&gt;
&lt;tr&gt;
&lt;td class="bodytext"&gt;
&lt;table width="600" border="0" cellspacing="0" cellpadding="5"&gt;
&lt;tr&gt;
&lt;td class="bodytext"&gt;&lt;form name="form1" method="post" action="search.asp"&gt;
&lt;p&gt; Keywords:
&lt;input name="keywords" type="text" class="textbox" id="keywords" value="&lt;% = Request.QueryString("keywords" %&gt;"&gt;
&lt;input type="submit" name="Submit" value="Search" class="button"&gt;
&lt;input name="search" type="hidden" id="search" value="go"&gt;
&lt;/p&gt;
&lt;/form&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;% IF Request.QueryString("search" = "go" THEN %&gt;
&lt;% IF NOT rsPageListings.EOF THEN %&gt;
&lt;table width="600" border="0" cellspacing="0" cellpadding="5"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;table width="590" height="26" border="0" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td class="bodytext"&gt;&lt;strong&gt;Results&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;%
IF record_offset &lt;&gt; 1 THEN
DO UNTIL CInt(recordnumber)=CInt(record_offset-1)
recordnumber = recordnumber + 1
rsPageListings.movenext
LOOP
END IF
DO WHILE recordnumber &lt; (record_offset + listing_per_page - 1) AND NOT rsPageListings.EOF
recordnumber = recordnumber + 1

percent_fraction = (rsPageListings("KWC_" & new_KWC) / top_KWC)
percent_text = percent_fraction * 100
percent_text = FormatNumber(percent_text,0,-2,-2,-2)
percent_bar = percent_fraction * 120
percent_bar = FormatNumber(percent_bar,0,-2,-2,-2)
%&gt;
&lt;table width="590" height="1" border="0" cellpadding="0" cellspacing="0" bordercolor="#F4F4F4" bgcolor="#&lt;% = Application(ESM_LICENCE & "line_colour_1" %&gt;"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="../images/site/sm_designs/spacer.gif" width="1" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="590" height="18" border="0" cellpadding="0" cellspacing="0" bgcolor="#&lt;% = Application(ESM_LICENCE & "sub_page_header_bk" %&gt;"&gt;
&lt;tr&gt;
&lt;td width="125" class="subtitle"&gt;
&lt;table width="120" height="10" border="0" align="left" cellpadding="1" cellspacing="0" bgcolor="#000000"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;table width="&lt;% = percent_bar %&gt;" height="8" border="0" cellpadding="0" cellspacing="0" bgcolor="#94A6D6"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="../images/site/design/spacer.gif" width="1" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;strong&gt; &lt;/strong&gt;&lt;/td&gt;
&lt;td class="subtitle"&gt;&lt;strong&gt;
&lt;% =(rsPageListings("PAGE_NAVIGATION") %&gt;
&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="590" height="18" border="0" cellpadding="0" cellspacing="0" bgcolor="#&lt;% = Application(ESM_LICENCE & "sub_page_header_bk" %&gt;"&gt;
&lt;tr&gt;
&lt;td class="subtitle"&gt; &lt;table width="590" border="0" cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;
&lt;td class="bodytextsmall"&gt;&lt;strong&gt;Score:
&lt;% = (percent_text) %&gt;
&lt;/strong&gt; -
&lt;% = rsPageListings("KWC_" & new_KWC) %&gt;
keyword&lt;% IF rsPageListings("KWC_" & new_KWC) &gt; 1 THEN Response.Write("s" %&gt; found&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="590" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#&lt;% = Application(ESM_LICENCE & "line_colour_2" %&gt;"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="../images/site/sm_designs/spacer.gif" width="1" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="590" border="0" cellpadding="5" cellspacing="0" bgcolor="#&lt;% = Application(ESM_LICENCE & "sub_page_desc_bk" %&gt;"&gt;
&lt;tr&gt;
&lt;td class="bodytext"&gt; &lt;p&gt;
&lt;% =(rsPageListings("PAGE_DESC") %&gt;
&lt;a href="default.asp?p=&lt;% =(rsPageListings("PAGE_ID") %&gt;"&gt;
&lt;% = Application(ESM_LICENCE & "results_link" %&gt;
&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="590" height="1" border="0" cellpadding="0" cellspacing="0" bgcolor="#&lt;% = Application(ESM_LICENCE & "line_colour_3" %&gt;"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="../images/site/sm_designs/spacer.gif" width="1" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;table width="590" height="18" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"&gt;
&lt;tr&gt;
&lt;td class="subtitle"&gt;&lt;img src="../images/site/sm_designs/spacer.gif" width="1" height="1"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;%
rsPageListings.MoveNext
LOOP
%&gt;
&lt;table width="590" height="40" border="0" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td class="bodytext"&gt; &lt;div align="center"&gt;&lt;strong&gt;
&lt;%
IF recordnumber &gt; listing_per_page THEN
Response.Write "&lt;a href='search.asp?keywords=" & Request.QueryString("keywords" & "&search=go&o=" & record_offset - listing_per_page & "'&gt;&lt;&lt; Previous&lt;/a&gt;" & chr(13)
END IF

IF record_count &gt; listing_per_page THEN
FOR t = 1 TO CInt((record_count/listing_per_page)+0.49)
Response.Write " " & chr(13)
IF Cint(recordnumber)&lt;&gt;(listing_per_page*(t-1))+listing_per_page THEN
IF recordnumber &lt; (listing_per_page*(t)) AND recordnumber &gt; (listing_per_page*(t-1)) THEN
Response.Write(t)
ELSE
Response.Write "&lt;a href='search.asp?keywords=" & Request.QueryString("keywords" & "&search=go&o=" & listing_per_page * (t-1) + 1 & "'&gt;" & t & "&lt;/a&gt;"
END IF
ELSE
Response.Write(t)
END IF
NEXT
Response.Write chr(13)
END IF
IF record_count &gt; recordnumber THEN
Response.Write " &lt;a href='search.asp?keywords=" & Request.QueryString("keywords" & "&search=go&o=" & record_offset + listing_per_page & "'&gt;Next &gt;&gt;&lt;/a&gt;" & chr(13)
END IF
%&gt;
&lt;/strong&gt; &lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;% END IF %&gt;
&lt;% END IF %&gt;
&lt;% IF errortext &lt;&gt; "" THEN %&gt;
&lt;table width="600" border="0" cellpadding="5" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td class="bodytext"&gt;
&lt;% = errortext %&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;% END IF %&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;%
IF Request.QueryString("search" = "go" THEN
CALL CloseRecordSet(rsPageListings)
END IF
%&gt;
&lt;%
Response.Write(ary_page_template(1))
%&gt;
</font id=red>

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Copy the above into a page called search.asp. Format the sections to search, etc. I use a number of functions for getting record sets, building page, closing recordsets but you can replace them with your own. You will also see a number of Application variables I use, but you can take them out. Don't take the one out for 'DWS_KWC', it's important variable. Another important thing is: The table you are searching, you have to add 10 fields and name them... KWC_1, KWC_2, KWC_3....... KWC_10. This allows it to work. It might give you a starting block at least. I will write a proper tutorial when I have the time.

Good luck!!!

_________________________
WinXP : IIS 5.1 : StudioMX : ASP : VBScript
www.clicksdesign.com

Edited by - beano on 29 Nov 2002 10:53:02

Reply to this topic