473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with paging search results correctly

Hello,
I have set this script up to add paging to a search results page. Which on
the first page works fine. I calculates how many pages there should be
depending on the number of results returned from my search and how many
records I have set it to display per page. All great so far, HOWEVER.....

When you click the next link to see the next 10 results on the next page, it
just dumps the search details and pulls up all the records in the table! So
it finds 320 matching results, and then happily pages through them 10
records at a time, with both the next and prev links working!

I need to find a way of using this paging system in conjunction with the
search results, so if for example I search for "yellow" and I get 18
matching results with the first 10 being displayed on the first page, & I
then click next, I would like to see the last 8 results on the next page,
with the option to click 'prev' and see the previous 10 again. What actually
happens is that it shows me the first 10 matching results and says
displaying page 1 of 2, I click the 'next' link and it suddenly says showing
page 2 of 32 and starts listing all the records from the database starting
at record number 11 !

Please can anyone help me fix this?
Here is my current code:

================================================== =
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/connTS.asp" -->
<%
Dim rsCustSearch__testvar
rsCustSearch__testvar = "%"
If (Request.form("search") <> "") Then
rsCustSearch__testvar = Request.form("search")
End If
%>
<%
Dim rsCustSearch
Dim rsCustSearch_numRows
Const NumPerPage = 10

If Request.QueryString("page") = "" then
CurrentPage = 1 'We're on the first page
Else
CurrentPage = CInt(Request.QueryString("page"))
End If

Set rsCustSearch = Server.CreateObject("ADODB.Recordset")
rsCustSearch.CursorLocation = 3
rsCustSearch.CursorType = 3 'adOpenStatic
rsCustSearch.ActiveConnection = MM_connTS_STRING
rsCustSearch.Source = "SELECT ProductID, ProductName, Keywords, ProductPict
FROM Products WHERE Keywords LIKE '%" + Replace(rsCustSearch__testvar, "'",
"''") + "%' OR ProductName LIKE '%" + Replace(rsCustSearch__testvar, "'",
"''") + "%'"

rsCustSearch.LockType = 1
rsCustSearch.Open()

If Not rsCustSearch.EOF Then
rsCustSearch.MoveFirst
rsCustSearch.PageSize = NumPerPage
TotalPages = rsCustSearch.PageCount
maxrecs = rsCustSearch.Pagesize

'Set the absolute (current) page
rsCustSearch.AbsolutePage = CurrentPage
End If

Dim Count
%>
<%
Dim Repeat1__numRows
Dim Repeat1__index

Repeat1__numRows = -1
Repeat1__index = 0
rsCustSearch_numRows = rsCustSearch_numRows + Repeat1__numRows
%>
<%
' *** Recordset Stats, Move To Record, and Go To Record: declare stats
variables

Dim rsCustSearch_total
'Dim rsCustSearch_first
'Dim rsCustSearch_last

' set the record count
rsCustSearch_total = rsCustSearch.RecordCount

' set the number of rows displayed on this page
'If (rsCustSearch_numRows < 0) Then
' rsCustSearch_numRows = rsCustSearch_total
'Elseif (rsCustSearch_numRows = 0) Then
' rsCustSearch_numRows = 1
'End If

' set the first and last displayed record
'rsCustSearch_first = 1
'rsCustSearch_last = rsCustSearch_first + rsCustSearch_numRows - 1

' if we have the correct record count, check the other stats
'If (rsCustSearch_total <> -1) Then
' If (rsCustSearch_first > rsCustSearch_total) Then
' rsCustSearch_first = rsCustSearch_total
' End If
'If (rsCustSearch_last > rsCustSearch_total) Then
' rsCustSearch_last = rsCustSearch_total
' End If
'If (rsCustSearch_numRows > rsCustSearch_total) Then
' rsCustSearch_numRows = rsCustSearch_total
'End If
'End If
%>

<%
' *** Recordset Stats: if we don't know the record count, manually count
them

If (rsCustSearch_total = -1) Then

' count the total records by iterating through the recordset
rsCustSearch_total=0
While (Not rsCustSearch.EOF)
rsCustSearch_total = rsCustSearch_total + 1
rsCustSearch.MoveNext
Wend

' reset the cursor to the beginning
'If (rsCustSearch.CursorType > 0) Then
' rsCustSearch.MoveFirst
'Else
' rsCustSearch.Requery
'End If

' set the number of rows displayed on this page
If (rsCustSearch_numRows < 0 Or rsCustSearch_numRows > rsCustSearch_total)
Then
rsCustSearch_numRows = rsCustSearch_total
End If

' set the first and last displayed record
rsCustSearch_first = 1
rsCustSearch_last = rsCustSearch_first + rsCustSearch_numRows - 1

If (rsCustSearch_first > rsCustSearch_total) Then
rsCustSearch_first = rsCustSearch_total
End If
If (rsCustSearch_last > rsCustSearch_total) Then
rsCustSearch_last = rsCustSearch_total
End If

End If
%>
<%
Dim MM_paramName
%>
<%
' *** Go To Record and Move To Record: create strings for maintaining URL
and Form parameters

Dim MM_keepNone
Dim MM_keepURL
Dim MM_keepForm
Dim MM_keepBoth

Dim MM_removeList
Dim MM_item
Dim MM_nextItem

' add the URL parameters to the MM_keepURL string
For Each MM_item In Request.QueryString
MM_nextItem = "&" & MM_item & "="
If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then
MM_keepURL = MM_keepURL & MM_nextItem &
Server.URLencode(Request.QueryString(MM_item))
End If
Next

' add the Form variables to the MM_keepForm string
For Each MM_item In Request.Form
MM_nextItem = "&" & MM_item & "="
If (InStr(1,MM_removeList,MM_nextItem,1) = 0) Then
MM_keepForm = MM_keepForm & MM_nextItem &
Server.URLencode(Request.Form(MM_item))
End If
Next

' create the Form + URL string and remove the intial '&' from each of the
strings
MM_keepBoth = MM_keepURL & MM_keepForm
If (MM_keepBoth <> "") Then
MM_keepBoth = Right(MM_keepBoth, Len(MM_keepBoth) - 1)
End If
If (MM_keepURL <> "") Then
MM_keepURL = Right(MM_keepURL, Len(MM_keepURL) - 1)
End If
If (MM_keepForm <> "") Then
MM_keepForm = Right(MM_keepForm, Len(MM_keepForm) - 1)
End If

' a utility function used for adding additional parameters to these strings
Function MM_joinChar(firstItem)
If (firstItem <> "") Then
MM_joinChar = "&"
Else
MM_joinChar = ""
End If
End Function
%>
<html><!-- InstanceBegin template="/Templates/home.dwt"
codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Search Results</title>
</head>

<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0"
marginwidth="0" marginheight="0">
<form action="SearchResults.asp" method="post" name="frmsearch"
id="frmsearch">

<input name="search" type="text" class="formelement"
id="search">
<input name="Submit" type="submit" class="formelement"
value="Submit">form><div align="left"><!-- InstanceBeginEditable
name="body" -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"><div align="center"><img src="images/search.jpg"><br>
<font face="Arial, Helvetica, sans-serif"><strong>Your
Search </strong><font size="3">returned <%=(rsCustSearch_total)%>
results</font></font></div>
<font size="2" face="Verdana">Displaying page <%=CurrentPage%>
of <%=TotalPages%>:</font></b> &nbsp;
<%
'Display PREV page link, if appropriate
If Not CurrentPage = 1 Then
Response.Write "<a href='" & ScriptName & "?page=" & CurrentPage - 1 &
"'>Prev</a> | "
Else
Response.Write "Prev | "
End If

'Display NEXT page link, if appropriate
If Not CurrentPage = TotalPages Then
Response.Write "<a href='" & ScriptName & "?page=" & CurrentPage + 1 &
"'>Next</a>"
Else
Response.Write "Next"
End If
%></td>
</tr>
<%
'Loop to display data on current page.
Do While Not rsCustSearch.EOF and Count < rsCustSearch.PageSize OR
howmanyrecs>=maxrecs
%>
<tr>
<td width="49%"><div
align="right"><b><%=(rsCustSearch.Fields.Item("Pro ductName").Value)%><br><A
href="ProductDetails.asp?<%= MM_keepURL & MM_joinChar(MM_keepURL) &
"ProductID=" & rsCustSearch.Fields.Item("ProductID").Value %>">View Item
Details</a></div></td>
<td width="51%"><A href="ProductDetails.asp?<%= MM_keepURL &
MM_joinChar(MM_keepURL) & "ProductID=" &
rsCustSearch.Fields.Item("ProductID").Value %>"><img
src="http://www.bathchain.co.uk/images/<%=(rsCustSearch.Fields.Item("Product
Pict").Value)%>" border="0" width="100" height="100"></A></td>
</tr>
<%

rsCustSearch.MoveNext
Count = Count + 1
Loop
%>
</table>
<!-- InstanceEndEditable --><br>
</div>
</body>
<!-- InstanceEnd --></html>
<%
rsCustSearch.Close()
Set rsCustSearch = Nothing
%>
================================================== =

It is basicly losing the search recordset details when I click 'next' or
'prev' & I need someone to show me how to get round this!

THANKYOU in advance for your help.......
Joseph
CharitiesOnline
Jul 19 '05 #1
2 3511
"CharitiesOnline" <ch*************@hotmail.com> wrote in message
news:Ia**************@news-binary.blueyonder.co.uk...
Hello,
I have set this script up to add paging to a search results page. Which on the first page works fine. I calculates how many pages there should be
depending on the number of results returned from my search and how many records I have set it to display per page. All great so far, HOWEVER.....
When you click the next link to see the next 10 results on the next page, it just dumps the search details and pulls up all the records in the table! So it finds 320 matching results, and then happily pages through them 10
records at a time, with both the next and prev links working!

I need to find a way of using this paging system in conjunction with the search results, so if for example I search for "yellow" and I get 18
matching results with the first 10 being displayed on the first page, & I then click next, I would like to see the last 8 results on the next page, with the option to click 'prev' and see the previous 10 again. What actually happens is that it shows me the first 10 matching results and says
displaying page 1 of 2, I click the 'next' link and it suddenly says showing page 2 of 32 and starts listing all the records from the database starting at record number 11 !

Please can anyone help me fix this?


Please don't multi-post

Newsgroup Etiquette:
aspfaq.com/2081
Recordset Paging:
aspfaq.com/2120

HTH
-Chris Hohmann
Jul 19 '05 #2
And now for an answer to your actual question... :)

The problem is that the search criteria is not being passed along when
you page through the recordset. You're code is looking for
Reuqest.Form("seach"). Therefore you must either

1. Replace your previous/next links with a form that passes the search
criteria.

OR

2. Include the search criteria in the links and modify your code to
check for both Request.Form("search") and Request.Querystring("search")
HTH
-Chris


Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: enak | last post by:
I can not get my datagrid to page. I have a datagrid that I can sort 2 of the columns. This works great. I added paging and when I display the dg it shows 5 pages. (I am showing page numbers at...
0
by: aftab | last post by:
Hi As i am doing a search results page where we have to list lots of results using paging in ASP.NET. I have two different databases one from sqlserver and one from oracle. so i have to query the...
2
by: Calvin KD | last post by:
Hi everyone, I have a datagrid to display search results. The search results at this stage we only return the first 500 records to speed up the search. The problem is, if I turn the...
2
by: Arsalan Ahmad | last post by:
Hi, May be I am a newbie, or may be i dont have that much insight in following systems ..i.e. why i have some confusions as below: In many websites, when search is performed on some keywords...
0
by: lewis | last post by:
I have a simple web site that does a search in a table. On the top of the screen i have several fields that allow a user to type in data and a search button. In the click of the button i set the...
18
by: daveeboi | last post by:
When running the script below I am able to get all related table results and display them the way I want. When it came to my paging script I entered and limited the rows per page to 2 (for a 3...
0
by: jimmmeh | last post by:
Hey guys, I will try and keep this as concise as I can. Basically I am developing a site which uses a Wizard Control (dynamically generated from a database) as a search form linked to a results page...
1
by: russot00 | last post by:
I have 3 drop down menus that are used in a search to locate restaurants in a db. All of the drop down menus function, a search can be submitted with any combination of drop downs and the results are...
1
by: John A Grandy | last post by:
In regard to a GridView that must support searching, filtering, sorting, and paging ... There is a tradeoff in performing the sorting and paging in the database versus to creating a CLR sort...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.