Ver Mensaje Individual
  #102 (permalink)  
Antiguo 30/03/2005, 15:14
Avatar de pablinweb
pablinweb
 
Fecha de Ingreso: julio-2003
Mensajes: 283
Antigüedad: 20 años, 9 meses
Puntos: 0
Como paginar una consulta en SQL 2k y ORDER BY

Código:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<title>Paginacion</title>

<!--#include file="adovbs.inc" -->

<%

'*********************************************************
'Se supone que hay creada y abierta una conexión llamda Conn
'*********************************************************

gOrder = request.QueryString("ORDER")
if gOrder = "" then
	SQL = "Select * from Tabla"
else
	SQL = "Select * from Tabla order by " & gOrder
end if
Dim mostrar 'cantidad de registros a mostrar por página
Dim cant_paginas 'cantidad de páginas que recibimos
Dim pagina_actual 'La página que mostramos
Dim registro_mostrado 'Contador utilizado para mostrar las páginas
Dim I 'Variable Loop

mostrar = 10 ' Pueden modificar este número para cambiar la cantidad de registros por página

' IF para saber que página mostrar
If Request.QueryString("page") = "" Then
	pagina_actual = 1
Else
	pagina_actual = CInt(Request.QueryString("page"))
End If

Set RS = Server.CreateObject("ADODB.Recordset")
RS.PageSize = mostrar
RS.CacheSize = mostrar

' Abrimos la tabla...
RS.Open SQL, Conn,3,1
'contamos las páginas que se formaron con la variable mostrar.
cant_paginas = RS.PageCount

' Si el pedido de página cae afuera del rango,
' lo modificamos para que caiga adentro
If pagina_actual > cant_paginas Then pagina_actual = cant_paginas
If pagina_actual < 1 Then pagina_actual = 1

' Nos movemos a la página elegida
If cant_paginas > 0 Then
	RS.AbsolutePage = pagina_actual
End IF
%>
</head>

<body>
<table width="100%" border="1" align="center">
<% if Rs.eof then %>
	<tr>
      <th>No Hay Registros</th>
    </tr>
<% else %>
	    <tr>
	  	  <td></td>
	      <td>
		  	Página <%= pagina_actual %> de <%= cant_paginas %>
		  </td>
    	</tr>
		<tr>
	  	  <th>
		  	<a href="Main.asp?ORDER=ID">Id</a>
		  </th>
	      <th>
		  	<a href="Main.asp?ORDER=Descripcion">Descripci&oacute;n</a>
		  </th>
    	</tr>
	<% do while registro_mostrado < mostrar And not Rs.eof %>	
    	<tr>
	      <td>
		  	<% response.Write(Rs("ID")) %>
		  </td>
    	  <td>
			<% response.Write(Rs("Descripcion")) %>
	      </td>
    	</tr>
	<% 		Rs.movenext
			registro_mostrado = registro_mostrado + 1
	   loop 
   end if
Rs.close
Set Rs = Nothing
Conn.close
Set Conn = Nothing
Link = "Main.asp?ORDER=" & gOrder
%>
</table>
<table width="850">
  <tr>
    <td>
	  <div align="center"></div>
	  <div align="right" class="style2">
        <div align="center">
          <%
	' Ahora mostramos los enlaces a las otras páginas con el resto de los registros...
	If pagina_actual > 1 Then
		If cant_paginas > 1 Then
			If Link = "" then %>
				    <a href="Main.asp?page=1">[<<]</a>
         <%	Else %>
				    <a href="<%=Link%>&page=1">[<<]</a>
         <%	End If
		End If
	End if
	If pagina_actual > 1 Then
		If Link = "" then %>
			    <a href="Main.asp?page=<%= pagina_actual - 1 %>">[<]</a>
          <%	Else %>
			    <a href="<%=Link%>&page=<%= pagina_actual - 1 %>">[<]</a>
          <%	End If
	End If

	' mostramos la paginacion por numeros de página
	if cant_paginas < 6 then
		fCant = 1
	else
		if pagina_actual < 4 then
			fCant = 1
		else
			if pagina_actual > 3 then
				fCant = pagina_actual - 2
			else
				fCant = pagina_actual
			end if
			if (cant_paginas - fCant) < 5 then
				fCant = cant_paginas - 4
			end if
		end if
	end if
	For I = fCant To cant_paginas
		J = J + 1
		If J > 5 Then exit For
		If I = pagina_actual Then %>
			    <%= I %>
     <%	Else 
			If Link = "" then %>
				    <a href="Main.asp?page=<%= I %>"><%= I %></a>
         <%	Else %>
			        <a href="<%=Link%>&page=<%= I %>"><%= I %></a>
         <%	End If
		End If
	Next 'I

	If pagina_actual < cant_paginas Then 
		If Link = "" then %>
		                <a href="Main.asp?page=<%= pagina_actual + 1 %>">[>]</a>
          <%	Else %>
		                <a href="<%=Link%>&page=<%= pagina_actual + 1 %>">[>]</a>
          <%	End If 
	End If 
	If pagina_actual < cant_paginas Then 
		If cant_paginas > 1 Then 
			If Link = "" then %>
		    	<a href="Main.asp?page=<%= cant_paginas + 1 %>">[>>]</a>
         <%	Else %>
                <a href="<%=Link%>&page=<%= cant_paginas + 1 %>">[>>]</a>
	     <%	End If 
		End If
	End if %>   
      </div>
    </div></td>
  </tr>
</table>
</body>
</html>