| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

08-06-04, 12:28
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
adding counts to search results
|
|
I found this on the net but it shows that he is using one ASP page for both search and response. I need it for two pages. The first for HTML coding and the second (response page) with ASP coding. What do I need to modify?
Code:
<%
' Declare our variables... always good practice!
Dim strURL ' The URL of this page so the form will work
' no matter what this file is named.
Dim cnnSearch ' ADO connection
Dim rstSearch ' ADO recordset
Dim strDBPath ' path to our Access database (*.mdb) file
Dim strSQL ' The SQL Query we build on the fly
Dim strSearch ' The text being looked for
' Retreive the URL of this page from Server Variables
strURL = Request.ServerVariables("URL")
' Retreive the term being searched for. I'm doing it on
' the QS since that allows people to bookmark results.
' You could just as easily have used the form collection.
strSearch = Request.QueryString("search")
'strSearch = Replace(strSearch, "'", "''")
' Since I'm doing this all in one page I need to see if anyone
' has searched for something. If they have we hit the DB.
' O/W I just show the search form and quit.
%>
<p>Search our sample db by first or last name. (% returns all)</p>
<form action="<%= strURL %>" method="get">
<input name="search" value="<%= strSearch %>" />
<input type="submit" />
</form>
<p>[Try 'am' or 'er' for an example]</p>
<%
If strSearch <> "" Then
' MapPath of virtual database file path to a physical path.
' If you want you could hard code a physical path here.
strDBPath = Server.MapPath("database.mdb")
' Create an ADO Connection to connect to the sample database.
' We're using OLE DB but you could just as easily use ODBC or a DSN.
Set cnnSearch = Server.CreateObject("ADODB.Connection")
' This line is for the Access sample database:
'cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
' We're actually using SQL Server so we use this line instead:
cnnSearch.Open "Provider=SQLOLEDB;Data Source=10.2.1.214;" _
& "Initial Catalog=samples;User Id=samples;Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;"
' Build our query based on the input.
strSQL = "SELECT last_name, first_name, sales " _
& "FROM sample " _
& "WHERE last_name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR first_name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "ORDER BY last_name;"
' Execute our query using the connection object. It automatically
' creates and returns a recordset which we store in our variable.
Set rstSearch = cnnSearch.Execute(strSQL)
' Display a table of the data in the recordset. We loop through the
' recordset displaying the fields from the table and using MoveNext
' to increment to the next record. We stop when we reach EOF.
' For fun I'm combining some fields and showwing you can do more then
' just spit out the data in the form it is in in the table.
%>
<table border="1">
<tr>
<th>Name</th>
<th>Sales</th>
</tr>
<%
Do While Not rstSearch.EOF
%>
<tr>
<td><%= rstSearch.Fields("first_name").Value %> <%= rstSearch.Fields("last_name").Value %></td>
<td><%= rstSearch.Fields("sales").Value %></td>
</tr>
<%
rstSearch.MoveNext
Loop
%>
</table>
<%
' Close our recordset and connection and dispose of the objects
rstSearch.Close
Set rstSearch = Nothing
cnnSearch.Close
Set cnnSearch = Nothing
End If
' That's all folks! See it's really not all that hard.
%>
|
|

08-09-04, 21:03
|
|
Guru
|
|
Join Date: Jun 2003
Location: USA
Posts: 1,032
|
|
Perhaps start by changing this:
strURL = Request.ServerVariables("URL")
To be more like this instead:
strURL = "myresponsepage.asp"
|
|

08-09-04, 21:13
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
Oh- I've changed that. Here is the new code but there's error at bottom of page
|
|
Code:
<%
Option Explicit
'************************************************************************************
'* Declaration section
'************************************************************************************
' Mode contstants
Const MODE_DEFAULT = 1
Const MODE_RESULTS = 2
Const DB_NAME = "kjv.mdb" ' Name of our database file
Const SCRIPT_NAME = "bible5.asp" ' Name of this script
Const RECORDS_PER_PAGE = 20 ' Number of records per page
Dim nMode ' Current Mode
'************************************************************************************
'* End of Declaration section
'************************************************************************************
'************************************************************************************
'* Main section
'************************************************************************************
' Find out what mode we are in
nMode = CLng(Request.QueryString("Mode"))
' Depending on our mode we will do different things
Select Case nMode
Case MODE_RESULTS
' This is where all the results will show
ShowResults
Case Else ' This one is for MODE_DEFAULT or invalid modes all the same
' By default display the search form
ShowSearchForm
End Select
'************************************************************************************
'* End of Main section
'************************************************************************************
'************************************************************************************
'* Functions section
'************************************************************************************
Private Function GetConnectionString()
GetConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath(DB_NAME) & ";" & _
"UID=;PWD=;"
End Function
Public Function OutputPageHeader()
%>
<HTML>
<HEAD><TITLE>ADO Recordset Paging Sample</TITLE></HEAD>
<BODY>
<H2>Search the bible, the Wheel of God</H2>
<H3><A HREF="<%=SCRIPT_NAME%>">Back Home</A></H3>
<%
End Function
Public Function OutputPageFooter()
%>
</BODY>
</HTML>
<%
End Function
Private Function ShowSearchForm()
OutputPageHeader
%>
<!--
This form will direct user to itself with MODE_RESULTS mode
-->
<FORM ACTION="<%=SCRIPT_NAME%>" METHOD="GET">
Look for: <INPUT TYPE="text" NAME="Keyword" VALUE=""> <INPUT TYPE="submit" VALUE=" Search "><input type="reset">
<INPUT TYPE="hidden" NAME="Mode" VALUE="<%=MODE_RESULTS%>"><p>
If you search number write 001 instead of 1 and 022 instead of 22
</td>
<br>
<INPUT TYPE="CheckBox" NAME="book" VALUE="Book">Book<br>
<INPUT TYPE="CheckBox" NAME="book_spoke" VALUE="Book Spoke">Book Spoke<br>
<INPUT TYPE="CheckBox" NAME="book_title" VALUE="Book Title">Book Title<br>
<INPUT TYPE="CheckBox" NAME="chapter" VALUE="Chapter">Chapter<br>
<INPUT TYPE="CheckBox" NAME="chapter_spoke" VALUE="Chapter Spoke">Chapter Spoke<br>
<INPUT TYPE="CheckBox" NAME="verse" VALUE="Verse">Verse<br>
<INPUT TYPE="CheckBox" NAME="verse_spoke" VALUE="Verse Spoke">Verse Spoke<br>
<INPUT TYPE="CheckBox" NAME="text_data" VALUE="Text" CHECKED>Text<br>
</tr>
</FORM>
<%
OutputPageFooter
End Function
' This function will display the results of the search
Private Function ShowResults()
Dim strConn ' Database connection string
Dim SQL ' String that will have our SQL statments
Dim RS ' Recordset object
Dim Keyword ' Keyword for search
Dim nRecCount ' Number of records found
Dim nPageCount ' Number of pages of records we have
Dim nPage ' Current page number
Dim iCounter 'for the checkboxes
' Let's see what page are we looking at right now
nPage = CLng(Request.QueryString("Page"))
' Let's see what user wants to search for today :)
Keyword = Trim(Request.QueryString("Keyword"))
' define our SQL statment
' we will be looking for all the records in tblItem table
' where ItemName contains our Keyword
' do not forget to fix tick marks (single quotes) in our Keyword
SQL = "SELECT * FROM bible WHERE "
' Create our connection string
strConn = GetConnectionString()
If request.QueryString("book")="yes" then
Sql = Sql & "book LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("book_spoke")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "book_spoke LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("book_title")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "book_title LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("chapter")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "chapter LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("chapter_spoke")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "chapter_spoke LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("verse")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "verse LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("verse_spoke")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "verse_spoke LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("text_data")="yes" then
If iCounter > 0 Then
Sql = Sql & " AND "
End If
Sql = Sql & "text_data LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
' Time to create and open recordset
Set RS = Server.CreateObject("ADODB.Recordset")
RS.CursorLocation = 3 ' adUseClient
RS.Open SQL, strConn ' adOpenKeyset CursorType
' Start outputing HTML
OutputPageHeader
' Did we find anything?
If Not RS.Eof Then
' Let's deal with our findings
' Get records count
nRecCount = RS.RecordCount
' Tell recordset to split records in the pages of our size
RS.PageSize = RECORDS_PER_PAGE
' How many pages we've got
nPageCount = RS.PageCount
' Make sure that the Page parameter passed to us is within the range
If nPage < 1 Or nPage > nPageCount Then
' Ops - bad page number
' let's fix it
nPage = 1
End If
' Time to tell user what we've got so far
Response.Write nRecCount & " records found matching """ & Keyword & """.<br>"
Response.Write nPageCount & " pages of results.<br>"
Response.Write "Current page is " & nPage & ".<p>"
' Give user some navigation
' first page
' we link to this page with Page parameter = 1
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & 1 & _
""">First Page</A>"
Response.Write " "
' Previous Page
' we link to this page with Page parameter = Current Page - 1
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & nPage - 1 & _
""">Prev. Page</A>"
Response.Write " "
' Next Page
' we link to this page with Page parameter Current Page + 1
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & nPage + 1 & _
""">Next Page</A>"
Response.Write " "
' Last Page
' we link to this page with Page parameter = nPageCount
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & nPageCount & _
""">Last Page</A>"
' Start Results
Response.Write "<p><b>These are the results:</b><br>" & String(20,"-")
' Position recordset to the page we want to see
RS.AbsolutePage = nPage
' Let's output our records
' Loop through records until it's a next page or End of Records
%>
<table BORDER="0" width="100%" cellpadding="3">
<tr>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Book </font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Book Spoke </font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Book Title </font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Chapter </font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Chapter Spoke</font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Verse </font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Verse Spoke</font></th>
<th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Text </font></th>
</tr>
<%
Do While Not (RS.Eof OR RS.AbsolutePage <> nPage)
%> '324
<tr>
<td><%=rs("book")%> 
</td>
<td><%=rs("book_spoke")%>
</td>
<td><%=rs("book_title")%>
</td>
<td><%=rs("chapter")%>
</td>
<td><%=rs("chapter_spoke")%>
</td>
<td><%=rs("verse")%>
</td>
<td><%=rs("verse_spoke")%>
</td>
<td><%=rs("text_data")%>
</td>
</tr>
' Move on to the next record
RS.MoveNext
Loop
Else
' We did not find anything
Response.Write "Nothing found. Try again.<p><A HREF=""" & SCRIPT_NAME & """>Back</A>"
End If
</table>
<%End If%>
<%End If%>
' Be nice - close the recordset
RS.Close
' Finish this page
OutputPageFooter
End Function
'************************************************************************************
'* End of Functions section
'************************************************************************************
%>
Quote:
Microsoft VBScript compilation error '800a0400'
Expected statement
/bible5.asp, line 355
End If
^
|
|
|

08-09-04, 22:16
|
|
Guru
|
|
Join Date: Jun 2003
Location: USA
Posts: 1,032
|
|
<<
/bible5.asp, line 355
>>
I'd suggest correcting the code on line 355:
OutputPageFooter
Perhaps make it more like this instead:
Call OutputPageFooter()
|
|

08-09-04, 22:24
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
that didn't correct line 355
|

08-09-04, 22:50
|
|
Guru
|
|
Join Date: Jun 2003
Location: USA
Posts: 1,032
|
|
Perhaps get rid of those extra End If's at the end:
<%End If%>
<%End If%>
|
|

08-09-04, 23:08
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
this is what it says
Quote:
Microsoft VBScript compilation error '800a0400'
Expected statement
/bible5.asp, line 51
end select
^
|
Code:
<%
Option Explicit
'************************************************* ********
'* Declaration section
'************************************************* ********
' Mode contstants
Const MODE_DEFAULT = 1
Const MODE_RESULTS = 2
Const DB_NAME = "kjv.mdb" ' Name of our database file
Const SCRIPT_NAME = "bible5.asp" ' Name of this script
Const RECORDS_PER_PAGE = 20 ' Number of records per page
Dim nMode ' Current Mode
'************************************************* ********
'* End of Declaration section
'************************************************* ********
'************************************************* ********
'* Main section
'************************************************* ********
' Find out what mode we are in
nMode = CLng(Request.QueryString("Mode"))
' Depending on our mode we will do different things
Select Case nMode
Case MODE_RESULTS
' This is where all the results will show
ShowResults
Case Else ' This one is for MODE_DEFAULT or invalid modes all the same
' By default display the search form
ShowSearchForm
End Select
'************************************************* ********
'* End of Main section
'************************************************* ***********************************
'************************************************* ***********************************
'* Functions section
'************************************************* ***********************************
' This function will generate our connection string
' it assumes that Access database is in the same folder as this script
Private Function GetConnectionString()
GetConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath(DB_NAME) & ";" & _
"UID=;PWD=;"
End Function
|
|

08-09-04, 23:22
|
|
Guru
|
|
Join Date: Jun 2003
Location: USA
Posts: 1,032
|
|
Perhaps check all your code (especially in ShowResults and ShowSearchForm) to be sure that everytime you have an If that you also have an End If and that your Select statements look good...
|
|

08-20-04, 19:51
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
Paging problems. ShowResult()... Private sub? Call? Public?
The checkboxes and every thing are working. Now the paging is an issue because the code that I started and the code within paging seem quite different.
One had
Set dbGlobalWeb = Server.CreateObject("ADODB.Connection")
the other
Set RS = Server.CreateObject("ADODB.Recordset")
What's the difference?
Anyways the error shows this:
Quote:
Microsoft VBScript compilation error '800a03ea'
Syntax error
/amos13.asp, line 88
Call Private Sub ShowResults()
|
The code is:
Code:
<%
' This function will display the results of the search
Private Sub ShowResults()
Dim strConn ' Database connection string
Dim SQL ' String that will have our SQL statments
Dim RSGlobalWeb ' Recordset object
Dim Keyword ' Keyword for search
Dim nRecCount ' Number of records found
Dim nPageCount ' Number of pages of records we have
Dim nPage ' Current page number
Dim iCounter
Dim iLoopCount
Dim aRecTypes
Dim spoke ' For dropdown
' Let's see what page are we looking at right now
nPage = CLng(Request.QueryString("Page"))
' Let's see what user wants to search for today :)
Keyword = Trim(Request.QueryString("Keyword"))
spoke = Request.Querystring("spoke")
' define our SQL statement
' we will be looking for all the records in tblItem table
' where ItemName contains our Keyword
' do not forget to fix tick marks (single quotes) in our Keyword
SQL = "SELECT * FROM bible WHERE "
strConn = GetConnectionString()
'Set dbGlobalWeb = Server.CreateObject("ADODB.Connection")
'dbGlobalWeb.Open(GetConnectionString) taken out because I though it would be irrelevant
iCounter = 0
If request.QueryString("book")="yes" then
SQL = SQL & "book LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("book_title")="yes" then
If iCounter > 0 Then
SQL = SQL & " OR "
End If
SQL = SQL & "book_title LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("chapter")="yes" then
If iCounter > 0 Then
SQL = SQL & " OR "
End If
SQL = SQL & "chapter LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("verse")="yes" then
If iCounter > 0 Then
SQL = SQL & " OR "
End If
SQL = SQL & "verse LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("text_data")="yes" then
If iCounter > 0 Then
SQL = SQL & " OR "
End If
SQL = SQL & "text_data LIKE '%" & Keyword & "%'"
iCounter = iCounter + 1
end if
If request.QueryString("book_spoke")="Book_Spoke" then
If iCounter > 0 Then
SQL = SQL & " AND "
End If
SQL = SQL & "book_spoke = '" & spoke & "'"
iCounter = iCounter + 1
end if
If request.QueryString("chapter_spoke")="Chapter_Spoke" then
If iCounter > 0 Then
SQL = SQL & " AND "
End If
SQL = SQL & "chapter_spoke = '" & spoke & "'"
iCounter = iCounter + 1
end if
If request.QueryString("verse_spoke")="Verse_Spoke" then
If iCounter > 0 Then
SQL = SQL & " AND "
End If
SQL = SQL & "verse_spoke = '" & spoke & "'"
iCounter = iCounter + 1
end if
If Trim(Request.QueryString("recordType")) <> "" Then
aRecTypes = Split(Request.QueryString("recordType"), ",")
If IsArray(aRecTypes) Then 'This is a bit redundant, but it can't hurt
SQL = SQL & " AND ("
For iLoopCount = 0 To UBound(aRecTypes)
If iLoopCount <> 0 Then
SQL = SQL & " OR "
End If
SQL = SQL & " recordType = '" & trim(aRecTypes(iLoopCount)) & "'"
Next
End If
SQL = SQL & ")"
End If
' Time to create and open recordset
Set RS = Server.CreateObject("ADODB.Recordset")
RS.CursorLocation = 3 ' adUseClient
Response.Write sql
RS.Open SQL, strConn ' adOpenKeyset CursorType
' Start outputing HTML
call OutputPageHeader()
' Did we find anything?
If Not RS.Eof Then
' Let's deal with our findings
' Get records count
nRecCount = RS.RecordCount
' Tell recordset to split records in the pages of our size
RS.PageSize = RECORDS_PER_PAGE
' How many pages we've got
nPageCount = RS.PageCount
' Make sure that the Page parameter passed to us is within the range
If nPage < 1 Or nPage > nPageCount Then
' Ops - bad page number
' let's fix it
nPage = 1
End If
' Time to tell user what we've got so far
Response.Write nRecCount & " records found matching """ & Keyword & """.<br>"
Response.Write nPageCount & " pages of results.<br>"
Response.Write "Current page is " & nPage & ".<p>"
' Give user some navigation
' first page
' we link to this page with Page parameter = 1
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & 1 & _
""">First Page</A>"
Response.Write " "
' Previous Page
' we link to this page with Page parameter = Current Page - 1
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & nPage - 1 & _
""">Prev. Page</A>"
Response.Write " "
' Next Page
' we link to this page with Page parameter Current Page + 1
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & nPage + 1 & _
""">Next Page</A>"
Response.Write " "
' Last Page
' we link to this page with Page parameter = nPageCount
Response.Write "<A HREF=""" & SCRIPT_NAME & _
"?Keyword=" & Keyword & _
"&Mode=" & MODE_RESULTS & _
"&Page=" & nPageCount & _
""">Last Page</A>"
' Start Results
Response.Write "<p><b>These are the results:</b><br>" & String(50,"-")
' Position recordset to the page we want to see
RS.AbsolutePage = nPage
' Let's output our records
' Loop through records until it's a next page or End of Records
%>
.....End Sub at the end
|
|

08-22-04, 14:58
|
|
Useless...
|
|
Join Date: Jul 2003
Location: SoCal
Posts: 721
|
|
Is that all of the code on the page amos13.asp? Line 88 doesn't show anything of concern.
__________________
That which does not kill me postpones the inevitable.
|
|

08-22-04, 15:43
|
|
Registered User
|
|
Join Date: Jul 2004
Posts: 494
|
|
|
here is the entire code
|

08-22-04, 19:03
|
|
Useless...
|
|
Join Date: Jul 2003
Location: SoCal
Posts: 721
|
|
Code:
' This function will display the search form
Private Sub ShowSearchForm()
call OutputPageHeader()
%>
<!--
This form will direct user to itself with MODE_RESULTS mode
-->
<%
' This function will display the results of the search
Call Sub ShowResults()
.
.
.
End Sub
End Sub
You can't nest subroutines... and if you're doing "Call Sub ShowResults()" to execute this subroutine, you do not need "Sub" in there. It should just be "Call ShowResults()".
__________________
That which does not kill me postpones the inevitable.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|