Programming Tools
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingProgramming Tools

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 3rd, 2002, 08:12 AM
assigned assigned is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Atlanta
Posts: 9 assigned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How do You do a PREVIOUS and NEXT links?

That is, I have a db that users search to return records. However, I want them to see only 10 per page and then, a NEXT 10 link that will display the next 10 results.
I have tried using LIMIT , but it only returns only the 10 and no way for them to see the remaining results.
I am using Asp, MS SQL 7 on WINNT.


cheers

assigned

Reply With Quote
  #2  
Old June 3rd, 2002, 06:06 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Hey.
Well we've written an article on paging with PHP and MySQL, which you can see here: http://www.devarticles.com/art/1/110. Can you "read" PHP to convert what the article talks about into ASP and MS SQL?

It should give you an idea, but because MySQL supports LIMIT and SQL Server only supports TOP, it may be a bit difficult to get your head around. If you can't understand it then let me know and I will get one of our authors to write an article on it for you

Reply With Quote
  #3  
Old June 3rd, 2002, 07:26 PM
assigned assigned is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Atlanta
Posts: 9 assigned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes, you guessed right. it's not easy to convert this php stuff to ASP. This is a FAQ on many site ASP sites and no article written seem to have addressed it in Plain English. I am looking forward to the article.
And if you are feeling lucky, how about doing another on how to do this:
Display first paragraph of an article and then .......more.[when MORE is clicked, the entire entire can yhen be read] how to do it in ASP

cheers
assigned

Last edited by assigned : June 3rd, 2002 at 07:37 PM.

Reply With Quote
  #4  
Old June 4th, 2002, 06:02 PM
PabstER PabstER is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 26 PabstER User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
OK, well we've added the article to the list for this week and we will be talking with our writers tommorow. It should be on the site in a couple of days

Reply With Quote
  #5  
Old June 14th, 2002, 08:27 PM
DavidM DavidM is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 78 DavidM User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Was the article on displaying the first paragraph and the [read more...] ever written? I don't recall seeing it....

Thanks!

Reply With Quote
  #6  
Old June 14th, 2002, 08:52 PM
assigned assigned is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Atlanta
Posts: 9 assigned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nope, they never did. I have checked everyday and i am now learning how to do it with the help of various resources.
There are some very good write up on it at some sites. Try programmersResources.com and look for paging. sorry dont remember the exact article number, left it at work.

cheers,

assigned logic

Reply With Quote
  #7  
Old June 14th, 2002, 09:14 PM
assigned assigned is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Atlanta
Posts: 9 assigned User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ok when I posted this question on another forum, I got these replies from others.
I have not had time to try them 'cos my pc died and I have not finished rebuilding it. Please let me know which one works.
Also the link to an article that walks you step by step

=========================================
http://www.programmersresource.com/articles/paging.asp
================================================== =========
CODE BY ZENITH
====================

** mypage.asp

strSQL = "SELECT ID,name FROM table"
set RS = oConn.Execute(strSQL)

IF (RS.EOF) THEN
num_of_recs = 0
RS.Close
Set RS = Nothing
ELSE
results = RS.GetRows
num_of_recs = Ubound(results,2)
RS.Close
Set RS = Nothing
END IF

start = Request.Querystring("start")
end = Request.Querystring("end")

'if start & end are both "" -> we're on 1st page
'then records from 1-10 (table rows 0-9)

IF (start = "" AND end = "") THEN
FOR i = 0 TO 9
Response.Write("ID:" & results(0,i) & " Name:" & results(1,i) & "<br>")
NEXT

Response.Write("<a href=""mypage.asp?start=10&end=20"">Next 10</a>")
ELSE

'another page = not first one
FOR i = start TO end
Response.Write("ID:" & results(0,i) & " Name:" & results(1,i) & "<br>")
NEXT

Response.Write("<a href=""mypage.asp?start=" & start - 10 & "&end=" & end-10 & """>Previous 10</a>")
Response.Write("<a href=""mypage.asp?start=" & start + 10 & "&end=" & end+10 & """>Next 10</a>")

END IF

================================================== ==========================================

Code by GOOF
================================================== =================================

Dim currentPage, itemsPerPage, x, nextHTML, prevHTML
itemsPerPage = 10
currentPage = request.querystring("page")
If (currentPage = "") Then currentPage = 1

'connect to the database
'dbRS will be the recordset I use for the search results returned

dbRS.move((currentPage - 1) * itemsPerPage)

For x = 1 To itemsPerPage
If (dbRS.EOF = false) Then
'display the current item
dbRS.movenext
End If
Next

'if you aren't on page 1, construct a link for the previous page
If (currentPage > 1) Then
prevHTML = "<a href=""thisdoc.asp?page=" & (currentPage - 1) & """>Previous Page</a>"
End If

'if this isn't the end of the database, construct a link for the next page
If (dbRS = false) Then
nextHTML = "<a href=""thisdoc.asp?page=" & (currentPage + 1) & """>Next Page</a>"
End If

'close the database and clear the objects


=============================================
cheers,
assigned

Reply With Quote
  #8  
Old January 21st, 2004, 12:05 PM
sagarprathap sagarprathap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 sagarprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to sagarprathap
JSP to display 10 records per page

Hi,
I want to display 10 records per page using JSP,SQL Server 7.0,Tomcat.
Can any one help me in this regard.

Thanks in Advance
Sagar

Reply With Quote
  #9  
Old January 21st, 2004, 12:15 PM
sagarprathap sagarprathap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 sagarprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to sagarprathap
Can any one help me in this regard

Reply With Quote
  #10  
Old January 21st, 2004, 12:17 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
A general SQL statement would be:
SELECT records FROM table LIMIT 10

The paging logic is all here in this thread, its just a matter of converting it from PHP or ASP to JSP

Variables you want to pass to the page is the offset and the limit... offset being the first document you want to display, and limit being the number of variables past that... the actual paging system is simply an addition/multiplication table between limit and offset

Reply With Quote
  #11  
Old January 21st, 2004, 12:21 PM
sagarprathap sagarprathap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 sagarprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to sagarprathap
Thanks MadCowDzz
Can i get any sample code for this or can you specify any similar link so that i can modify and use it for my functionality

Reply With Quote
  #12  
Old January 21st, 2004, 12:55 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Actually, this is problematic in SQL Server because it doesn't support the LIMIT keyword. That was the initial problem. The logic for doing this is easy if you can limit your query. The easiest solution I can think of is to make sure you have an auto_increment (identity, in SQL Server) field that you order by and to keep track of the first and last ids used. So you'd "SELECT TOP 10 FROM table WHERE id > $stored_id" and then make sure you store the first and last ids in that batch to send to the next/prev links so that the next load of the page will know where to start.

Reply With Quote
  #13  
Old January 21st, 2004, 01:04 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Incidentally, the code snippets given above will work, but imagine you have a million records. All of them would have to be returned and iterated over in both bits of code given above, and that could make for a slow loading page. Consider this pseudocode alternative:

Code:

//Assumes id field is auto_increment/identity
//Set $start to the GET variable start
//Set $end to the GET variable end
//Set $flag to "prev" or "next" depending on which link was clicked.

$sql="SELECT TOP 10 FROM table ";
if($flag=="next"){
    $sql .= "WHERE id > $end"
}
else{
    $sql .= "WHERE id > ($start - 10)"
}

//Execute query stored in $sql
//Store the first and the last ids selected into $start and $end
//Print prev/next links, passing appropriate $flag and $start or $end as appropriate


This, or something very like it (it's untested) ensures that you only select ten records at a time, easing load on your server and making for a palatable wait-time for the user.

Reply With Quote
  #14  
Old January 21st, 2004, 01:09 PM
sagarprathap sagarprathap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 sagarprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to sagarprathap
dhouston
Thanks for your immediate replies,basically i am new to JSP as well as PHP.Can you give some code snippet in JSP to resolve this issue

Thanks
Sagar

Reply With Quote
  #15  
Old January 21st, 2004, 03:58 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I'm afraid not. I haven't touched jsp in a while and was flying by the seat of my pants when I did. The looping and variable assignment should be pretty easy, and it shouldn't be too big a production to look up the db connectivity and all. Good luck with it.

Reply With Quote
  #16  
Old January 23rd, 2004, 10:54 AM
sagarprathap sagarprathap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 5 sagarprathap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to sagarprathap
Displaying 10 records per page using JSP

Here goes my code and i am getting an error


Error:
javax.servlet.ServletException: javax/sql/RowSet
at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:249)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$Standard PipelineValveContext.invokeNext(StandardPipeline.j ava:643)
at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)

My Code is:
----------------------------------
<%@ page import="java.util.*, java.sql.*,com.sun.rowset.*,com.sun.rowset.CachedR owSetImpl" %>

<html>
<head>
<title>Physician Details Page</title>
</head>

<body bgcolor="#ffffee" text="blue">
<form target="_top" name=viewForm action="<%= request.getContextPath()%>/protected/dbtool_frameset.jsp" method="post">
<%! int numPages = 0; %>
<%
String columnName = "";
int count = 0;
int totalCols = 0;
int increment = 1;
ResultSetMetaData resultSetMetaData=null;

String numRowsString = (String) session.getAttribute("numRowsString");
int numRows = Integer.parseInt(numRowsString);
String startIndexString = request.getParameter("startIndex");
int startIndex = Integer.parseInt(startIndexString);

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//CachedRowSet cache = new CachedRowSet();
CachedRowSetImpl cache = new CachedRowSetImpl();

cache.setReadOnly(true);
cache.setUrl("jdbcdbc:faxDetails");
cache.setUsername("AtlantisUpdate");
cache.setPassword("Evenor");
cache.setCommand("SELECT * FROM PhysicianDetail");
cache.execute();
resultSetMetaData = cache.getMetaData();
totalCols = resultSetMetaData.getColumnCount();
%>
<table border=1 align=center width="100%">
<tr>
<%
for(int j=1; j<=totalCols; j++) {
columnName = (String) resultSetMetaData.getColumnName(j);
%><td>
<b><% out.print(columnName); %></b>
</td>
<%}%>
</tr>
<%
numPages = numRows / 10;
int remain = numRows % 10;

if(startIndex + 10 < numRows) {
increment = startIndex + 10;
}else{
increment = startIndex + remain;
}


cache.absolute(startIndex);

for(count = startIndex; count < increment; count++) {
%><tr><%
for(int i=1; i<=totalCols; i++) {
%><td><% out.println(cache.getString(i)); %></td><%
} // end for
%></tr><%
cache.next();
} // end for
%>
</table>
<br>
<br>
<%

%>
<table width = "100%">
<tr>
Displaying Records:
<% if(startIndex + 10 < numRows){%>
<%= " " + startIndex %> - <%= increment - 1 %>
<%}else{%>
<%= " " + startIndex %> - <%= numRows %>
<%}%>

<%if(startIndex != 1) {%>
<a href="<%=request.getContextPath()%>/protected/view_tableRecords.jsp?startIndex=<%=startIndex-10%>">Previous</a>
<%}%>

<%increment += 10;%>
<%if(startIndex + 10 <= numRows){%>
<a href="<%=request.getContextPath()%>/protected/view_tableRecords.jsp?startIndex=<%=startIndex+10%>">Next</a>
<%}%>
</tr>
</table>
<%
}catch(Exception exc){
out.println(exc.toString());
} // end try-catch
%>

</form>
</body>
</html>
--------------------------------------
Can any body help regardign this error

Thanks
Sagar Prathap
Attached Files
File Type: jsp