|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi all..
i have done a db search page... it works fine... and perfectly... i just need to add some more "user-friendly" features... for instance... showing that.. "record 1 to 10 of 10 pages" my current db search tells the user that how many pages there are in total and which page he is at... but i need to tell them that showing which records excatly... here it goes: ----------------------------------- <%@ Language = "VBScript" %> <% Option Explicit <!-- Variables Declaration --> Dim I ' Standard looping variable Dim strDBPath ' path to our Access database (*.mdb) file Dim strURL ' The URL of this page so the form will work ' no matter what this file is named. 'Page constants: Const iPageSize = 10 ' The size of our pages. Dim iPageCurrent ' The page we're currently on Dim iPageCount ' Number of pages of records Dim iRecordCount ' Count of the records returned Dim iRecordsShown 'Record Control <!-- ADO constants --> 'CursorTypeEnum Values Const adOpenForwardOnly = 0 Const adOpenKeyset = 1 Const adOpenDynamic = 2 Const adOpenStatic = 3 'LockTypeEnum Values Const adLockReadOnly = 1 Const adLockOptimistic = 3 'CommandTypeEnum Values Const adCmdText = 1 Const adCmdTable = 2 Const adUseClient = 3 <!-- ADO recordset variables --> Dim iadoConnection, iadoRecordset, iLogRecordset Dim LogSQL Dim strSQL ' The SQL Query we build on the fly Dim strSearch ' The text being looked for ' Retrieve the URL of this page from Server Variables strURL = Request.ServerVariables("URL") ' Retrieve 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, "'", "''") ' Retrieve page to show or default to the first If Request.QueryString("page") = "" Then iPageCurrent = 1 Else iPageCurrent = CInt(Request.QueryString("page")) End If %> <html> <head> <title>Search Page</title> </head> <body> <h1 align="center">Product Search</h1> <p> </p> <p> </p> <p>Search SKU or Product Name:</p> <form action="<%= strURL %>" method="GET"> <input name="Search" value="<%= strSearch %>" size="20" /> <input type="submit" name="UserSubmit" /> </form> <hr> <% If Len(strSearch) Then <!-- Database Location --> strDBPath = Server.MapPath("db/product.mdb") <!-- Database Connection --> Set iadoConnection = Server.CreateObject("ADODB.Connection") <1-- Database open mdb --> iadoConnection.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & strDBPath <!-- RecordSet Connection --> Set iadoRecordset = Server.CreateObject("ADODB.Recordset") <!-- END --> 'Set the cursor location property iadoRecordset.CursorLocation = adUseClient iadoRecordset.PageSize = iPageSize iadoRecordset.CacheSize = iPageSize ' Build our query based on the input. strSQL = "SELECT * FROM Products " _ & "WHERE SKU LIKE '%" & Replace(strSearch, "'", "''") & "%' " _ & "OR Name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _ & "ORDER BY ID ASC;" ' Open our recordset iadoRecordset.Open strSQL, iadoConnection, adOpenKeyset, adLockReadOnly, adCmdText ' Get a count of the number of records and pages ' for use in building the header and footer text. iRecordCount = iadoRecordset.RecordCount iPageCount = iadoRecordset.PageCount If iRecordCount <> 0 Then ' Move to the page we need to show. iadoRecordset.AbsolutePage = iPageCurrent ' Show a quick status line letting people know where they are: %> <p><%= iRecordCount %> Records Found.</p> <p>Displaying page <%= iPageCurrent %> of <%= iPageCount %>:</p> <% ' 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 showing you can do more then ' just spit out the data in the form it is in in the table. %> <table border="1" width="100%" id="SearchResults"> <tr bgcolor="#CCCCCC"> <th width="25%" align="center"> Product Name </th> <th width="25%" align="center"> Product Category </th> <th width="25%" align="center"> Product Price </th> <th width="25%" align="center"> Product Description </th> </tr> <% Do While iRecordsShown < iPageSize AND NOT iadoRecordset.EOF AND iPageCurrent dim introwcolor If intRowColor = 0 Then Response.Write "<tr bgcolor=""#EEEEEE"">" & vbCrLf intRowColor = 1 Else Response.Write "<tr bgcolor=""#CCCCCC"">" & vbCrLf intRowColor = 0 End if %> <!--<tr>--> <td width="25%" align="center"> <% = iadoRecordset.Fields("Name") %> </td> <td width="25%" align="center"> <% = iadoRecordset.Fields("Category") %> </td> <td width="25%" align="center"> <% = FormatCurrency(iadoRecordset.Fields("Price"), 2) %> </td> <td width="25%" align="center"> <% = iadoRecordset.Fields("Description") %> </td> </tr> <% iRecordsShown = iRecordsShown + 1 iadoRecordset.MoveNext Loop %> </table> <% ' Now we need to show our navigational links: ' Show "previous" and "next" page links which pass the page to ' view our search parameter. 'Show Navigational Links ONLY if more than 1 page If iPageCount > 1 Then %> <!-- Seach Page Navigation --> <p> <% If iPageCurrent > 1 Then Response.Write " <a href=""" & strURL & "?search=" & Server.URLEncode(strSearch) _ & "&page=" & (iPageCurrent - 1) & """><< Previous</a> " & vbCrLf Else Response.Write " << Previous " & vbCrLf End If 'Show page numbers: For I = 1 To iPageCount If I = iPageCurrent Then Response.Write " <b>" & I & "</b>" Else Response.Write " <a href=""" & strURL & "?search=" & Server.URLEncode(strSearch) _ & "&page=" & I & """>" & I & "</a>" End If 'I If i <> iPageCount Then Response.Write " | " Else Response.Write " " End If Response.Write vbCrLf Next 'I If iPageCurrent < iPageCount Then Response.Write " <a href=""" & strURL & "?search=" & Server.URLEncode(strSearch) _ & "&page=" & (iPageCurrent + 1) & """>Next >></a>" Else Response.Write " Next >>" End If %> </p> <% End If 'iPageCount > 1 Else ' Display no records error. %> <p>No records found. Please try again.</p> <% End If 'If iRecordCount <> 0 <!-- Clean up ADO variables --> 'Close RecordSet iadoRecordset.Close 'Reset RecordSet Variable Set iadoRecordset = Nothing 'Close Connection iadoConnection.Close 'Reset Connection Variable Set iadoConnection = Nothing End If 'If Len(strSearch) %> </body> </html> ---------------------------------- Thanks for all help in advance.... |
|
#2
|
|||
|
|||
|
Need help pls...
**URGENT** |
|
#3
|
|||
|
|||
|
cant this be done????!!!!
oh gosh..... |
|
#4
|
|||
|
|||
|
not sure if this'll do it, there may be more to it, but...
<%=(x_first)%> displays the starting record number <%=(x_last)%> displays the ending record number <%=(x_total)%> displays the total number of records where x is the db connection name, in this case iadoConnection |
|
#5
|
|||
|
|||
|
???
there is such a methid in asp? |
|
#6
|
|||
|
|||
|
Sorry about that, figures it couldn't have been that easy. The code below I've take directly from Dreamweaver, following the code connecting to the DB.
<% ' *** Recordset Stats, Move To Record, and Go To Record: declare stats variables Dim iadoConnection_total Dim iadoConnection_first Dim iadoConnection_last ' set the record count iadoConnection_total = iadoConnection.RecordCount ' set the number of rows displayed on this page If (iadoConnection_numRows < 0) Then iadoConnection_numRows = iadoConnection_total Elseif (iadoConnection_numRows = 0) Then iadoConnection_numRows = 1 End If ' set the first and last displayed record iadoConnection_first = 1 iadoConnection_last = iadoConnection_first + iadoConnection_numRows - 1 ' if we have the correct record count, check the other stats If (iadoConnection_total <> -1) Then If (iadoConnection_first > iadoConnection_total) Then iadoConnection_first = iadoConnection_total End If If (iadoConnection_last > iadoConnection_total) Then iadoConnection_last = iadoConnection_total End If If (iadoConnection_numRows > iadoConnection_total) Then iadoConnection_numRows = iadoConnection_total End If End If %> <% ' *** Recordset Stats: if we don't know the record count, manually count them If (iadoConnection_total = -1) Then ' count the total records by iterating through the recordset iadoConnection_total=0 While (Not iadoConnection.EOF) iadoConnection_total = iadoConnection_total + 1 iadoConnection.MoveNext Wend ' reset the cursor to the beginning If (iadoConnection.CursorType > 0) Then iadoConnection.MoveFirst Else iadoConnection.Requery End If ' set the number of rows displayed on this page If (iadoConnection_numRows < 0 Or iadoConnection_numRows > iadoConnection_total) Then iadoConnection_numRows = iadoConnection_total End If ' set the first and last displayed record iadoConnection_first = 1 iadoConnection_last = iadoConnection_first + iadoConnection_numRows - 1 If (iadoConnection_first > iadoConnection_total) Then iadoConnection_first = iadoConnection_total End If If (iadoConnection_last > iadoConnection_total) Then iadoConnection_last = iadoConnection_total End If End If %> Hopefully that'll do the trick. Then, use the code I gave you in the previous response to display the count. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > Database search functionality |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|