Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting items displayed per page? 2

Status
Not open for further replies.

lynxvoodoo

Technical User
Dec 28, 2002
14
GB
Hi!

Just out of interest I have been learning how to create a shopping cart, thanks to the various sources available off the net. The problem though is that I have been looking at the follwoing code which displays items from a database BUT how can I limit the number of items displayed per page? e.g 5 items per page with a "Previous" & "Next" button.

Any help would be very much appreciated.

Code --->

<!-- #include file=&quot;db.asp&quot; -->
<%
'===================================================
'a.shopKart 2.0
'(c) 1999-2000 Katrien Dermaut De Graeve - URLogy
'
'this page shows products for chosen category
'===================================================

Response.Buffer = true
Dim catid, strcat
catid = Request.QueryString(&quot;id&quot;)
strcat = Request.QueryString (&quot;cat&quot;)

If catid = &quot;&quot; OR (IsNumeric(catid) = false) Then
Response.Redirect &quot;default.asp&quot;
End if

Dim catname, productslist
sub productInfo(connObj,category)
q = chr(34)
set cmd = server.CreateObject(&quot;ADODB.Command&quot;)
cmd.ActiveConnection = connObj
cmd.CommandText = &quot;qryProdsCategory&quot;
cmd.CommandType = adCmdStoredProc
set param = cmd.CreateParameter(&quot;theCategory&quot;,adInteger,adParamInput,4)
cmd.Parameters.Append(param)
cmd(&quot;theCategory&quot;) = Cint(category)
set rs = server.CreateObject(&quot;ADODB.Recordset&quot;)
set rs = cmd.Execute

if not rs.EOF then
catname = rs(&quot;catdescription&quot;)
strHTML = &quot;<table border=0 cellPadding=4 cellSpacing=2 width='100%'>&quot;
strHTML = strHTML & &quot;<tr>&quot;

i = 1
while not rs.EOF
strHTML = strHTML & &quot;<td valign=top>&quot; & vbcrlf
strHTML = strHTML & &quot;<img src=&quot; & q& &quot;images/small/&quot; & rs(&quot;cimageurl&quot;) &q& &quot; align=&quot; & q& &quot;left&quot;& q & &quot;>&quot; & vbcrlf
strHTML = strHTML & &quot;<h4>&quot; & rs(&quot;cname&quot;) & &quot;</h4>&quot; & vbcrlf
strHTML = strHTML & &quot;<p><font size=-1><a href=&quot;&q&&quot;product.asp?id=&quot; & rs(&quot;catalogID&quot;) &q&&quot;>More &gt;&gt;&gt;</a></font></p>&quot; & vbcrlf
strHTML = strHTML & &quot;<p>Price: &quot; & FormatCurrency(rs(&quot;cprice&quot;),2) & &quot;<br>&quot; & vbcrlf
strHTML = strHTML & &quot;<form action=&quot;&q&&quot;addprod.asp&quot;&q&&quot; method=&quot;&q&&quot;POST&quot;&q&&quot; name=form&quot;&i&&quot; onSubmit=&quot;&q& &quot;return checkItems(form&quot;&i&&quot;)&quot; & q&&quot;>&quot; & vbcrlf
strHTML = strHTML & &quot;<input type=&quot;&q&&quot;hidden&quot;&q&&quot; name=&quot;&q&&quot;fproductid&quot;&q&&quot; value=&quot;&q & rs(&quot;catalogID&quot;)& q&&quot;>&quot; & vbcrlf
strHTML = strHTML & &quot;Quantity: <input maxLength=&quot;&q&&quot;1&quot;&q&&quot; name=&quot;&q&&quot;fquantity&quot;&q&&quot; size=2 value=&quot;&q&&quot;1&quot;&q&&quot;>&quot; & vbcrlf
strHTML = strHTML & &quot;<input type=&quot;&q&&quot;submit&quot;&q&&quot; value=&quot;&q&&quot;ORDER&quot;&q&&quot; name=&quot;&q&&quot;order&quot;&q&&quot; style=&quot;&q&&quot;font-family: Arial; color: #800000; font-weight: bold&quot;&q&&quot;>&quot;
strHTML = strHTML & &quot;</form></td>&quot; & vbcrlf
if (i mod 2) = 0 then
strHTML = strHTML & &quot;</tr><tr>&quot; & vbcrlf
end if
i = i + 1
rs.MoveNext
wend

strHTML = strHTML & &quot;<tr></table>&quot;
else
strHTML = &quot;Product information not found.&quot;
catname = &quot;Error&quot;
end if
productslist = strHTML
rs.Close
set rs = nothing
set cmd = nothing

end sub

%>
<html>
<head>
<title>Products</title>
<SCRIPT LANGUAGE=javascript>
<!--
function checkItems(formobj)
{
var quant = formobj.fquantity.value
if (isNaN(quant) == true || quant < 1 || quant > 9)
{
alert(&quot;Please enter a number between 1 and 9&quot;);
formobj.fquantity.focus();
return false
}else
{
return true;
}
}
//-->
</SCRIPT>
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;eposter.css&quot;>
</head>
<body>

<table border=&quot;0&quot; width=&quot;600&quot; cellpadding=&quot;4&quot;>
<tr>
<td width=&quot;100%&quot; colspan=&quot;2&quot; valign=&quot;top&quot;>
<h3><img src=&quot;images/eplogo2.gif&quot; alt=&quot;EPoster store&quot; width=&quot;187&quot; height=&quot;36&quot;>
<br><font face=&quot;Arial&quot;>
<%
'on error resume next
call openConn()
if dbc.errors.count > 0 then
Response.Write &quot;Error accessing database.&quot;
else
call productInfo(dbc,catid)
end if
call closeConn()
response.write catname
%>
</font></h3></td>
</tr>
<tr>
<td width=&quot;120&quot; bgcolor=&quot;#004080&quot; valign=&quot;top&quot;>
<!--#include file=&quot;navleft.htm&quot; --></td>
<td width=&quot;480&quot;>
<%= productslist %>
</td>
</tr>
</table>


</body>
</html>
 
hi there u just in time for the question i just got thru this piece of code here it goes .....
'=========================================================
'assuming the only problem is with the displaying of pages

strPageName = Request.serverVariables(&quot;SCRIPT_NAME&quot;)

if request(&quot;page&quot;) = &quot;1&quot; then
SomeVar = Request.Form(....)
strQuery = <<actual select stmt>>
else
strQuery = Request.Form(&quot;Qry&quot;)
end if

Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
with rs
.CursorLocation=3
.Open strQuery, ConLog,3 '3 is adOpenStatic
.PageSize = cint(25) '<- change this to any number u wish to
intPageCount = .PageCount
.AbsolutePage = request(&quot;page&quot;)
intCurrRec = .AbsolutePosition
intTotalRecords = .RecordCount
end with

for i = 1 to rs.PageSize
if not rs.EOF then
for each fld in rs.Fields
'use HTML/ASP to write to the page
next
rs.MoveNext
end if
next
'==== Above code prints on defined number of pages
'==== Write the Select stmt in a hidden field
<form name=&quot;strQry&quot; method=&quot;post&quot; >
<input type=&quot;hidden&quot; name=&quot;qry&quot; value=&quot;<%=strQuery%>&quot;>
</form>

'==== The Navigation Table
<table border=0 align=&quot;center&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot;>
<tr>
<%
if cint(intCurrPage)-cint(intPageCount)=0 then
'//// This is the Last Page
%>
<td><a href=&quot;Javascript:recallpage(1)&quot;>|<<a></td>
<td><a href=&quot;Javascript:recallpage(<%=int(intCurrPage)-1%>)&quot;><<<a></td>
<td class=psmallb>Page <%=intCurrPage%> of <%=intPageCount%></td>
<td>>></td>
<td>>|</td>
<%
elseif intCurrPage = 1 then
'//// This is the First Page
%>
<td>|<</td>
<td><<</td>
<td class=psmallb>Page <%=intCurrPage%> of <%=intPageCount%></td>
<td><a href=&quot;Javascript:recallpage(<%=int(intCurrPage)+1%>)&quot;>>><a></td>
<td><a href=&quot;Javascript:recallpage(<%=intPageCount%>)&quot;>>|<a></td>
<%
else
%>
<td><a href=&quot;Javascript:recallpage(1)&quot;>|<<a></td>
<td><a href=&quot;Javascript:recallpage(<%=int(intCurrPage)-1%>)&quot;><<<a></td>
<td class=psmallb>Page <%=intCurrPage%> of <%=intPageCount%></td>
<td><a href=&quot;Javascript:recallpage(<%=int(intCurrPage)+1%>)&quot;>>><a></td>
<td><a href=&quot;Javascript:recallpage(<%=intPageCount%>)&quot;>>|<a></td>
<%
end if
rs.Close
set rs= nothing
%>

<script language=&quot;JAVASCRIPT&quot;>
function recallpage(pg)
{
document.strQry.action = '<%=strPageName%>?page='+pg;
document.strQry.submit();
}
</script>

Hope this helps ,else write back !

[cheers]
Niraj[noevil]
 
Hi Niraj! Thanks for replying back to this message.

The info that you have supplied seems to be what I am after but its just that I am new to asp and creating a shopping cart may be abit advanced for me but its something that I have always wanted to learn.

I still want to implement the above and thus was wondering if help could be provided as to how I would go about adding your code to the code provided in my first message.

Any help will be very much appreciated.

Lynx
 
Lynx,

Are you also running into performance issues when loading your ASP page? It seems that you are concatenating a large string to finally output it at the end. This is a slow way of appending results for output. You should do more response.writes or something like in the post by Niraj, otherwise, you have to wait until your entire recordset is read before writing it to the response. ________________________________________________________________________
Are you trying to debug your ASP applications? See faq333-3255 for more details

regards,
Brian
 
hi lynx, thx for the star

i did not go thru ur code, gimme a day to go thru and i shall surely reply back on this.
however it was a mistake on my part not to comment the code. lemme organize this and i'll revert.

[cheers]
Niraj [noevil]
 
hi,
couple of things
firstly change the strHtml code to
Response.Write statements as said by Brian.

next is to store the Request.QueryString(&quot;id&quot;) values in a hidden field in the same form (the field names should be the same), this will keep the criteria accessible on all the pages.

rest is looping thru the recordset which is fairly simple...

The JavaScript recallpage is to submit the form (we just created with the similar fields) so the next page read the criteria.

the Navigation table is straight forward

if u require any assistance on this , kindly write back

[cheers]
Niraj [noevil]
 
Hi again.

I have decided to change the code to Response.write statements. Seems pretty straight forward (I think!!!..)

Thanks Brian for first suggesting this.

Niraj thanks for your help so far. I am still wanting to implement the page limitation script to the modified version. I understand the code and how to implement the navigation table but could you still provide help as how to implement the rest of the code to the modified code above.

Any further help would be greatly appreciated.

Have given both a star for your suggestions.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top