/ Forside / Teknologi / Udvikling / ASP / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
ASP
#NavnPoint
smorch 9259
Harlekin 1866
molokyle 1040
Steffanst.. 758
gandalf 657
smilly 564
gibson 560
cumano 530
MouseKeep.. 480
10  Random 410
Paging + Asp + Mysql
Fra : René Jørgensen


Dato : 22-05-11 15:02

Hej

Jeg har en brugerliste som efterhånden er lidt for lang til at
gennemskue på websiden, og vil derfor opsplitte den med paging
(asp + mysql). Er der nogen som kan anbefale et script eller
måske har noget kode liggende jeg kan arbejde videre på?

Mvh. René


--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

 
 
j p (30-05-2011)
Kommentar
Fra : j p


Dato : 30-05-11 15:26

Jeg bruger følgende kode

<%Option Explicit%>
<%
'Function designed to check if string contants only numbers
Function IsOnlyNumeric(str)
Dim ianRegEx
Set ianRegEx = New RegExp
ianRegEx.Pattern = "[^0-9]"
ianRegEx.Global = True
IsOnlyNumeric = (ianRegEx.Test(str) = False)
End Function
%>
<%
'Open database connection (Access)
Dim oConn, StrConn, SQL, oRS
Set oConn = Server.CreateObject("ADODB.Connection")
StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &
Server.MapPath("db\db1.mdb") & ";"
oConn.Open StrConn

'Count the number of records in the database
SQL = "SELECT Count(ID) AS RecordCount FROM tblData"
Set oRS = oConn.Execute(SQL)

Dim intRecordsPerPage, intNumberOfPages, intRecordCount
intRecordsPerPage = 5 'Number of records per page
intRecordCount = oRS("RecordCount") 'Number of records in
database

Dim intPageNumber
'Check to make sure page number in QueryString is valid
If IsOnlyNumeric(Request.QueryString("page")) = True And
Request.QueryString("page") <> "" Then
intPageNumber = Int(Request.QueryString("page"))
Else
intPageNumber = 1
End If

'Make sure there are records in the database
If Not oRS.EOF Then
'Calculate the number of pages based on how many records there
are divided by how many records per page
'By adding and subtracting 0.5 and using CInt() to round, this
will calculate the most pages needed for how many records there
are
intNumberOfPages = CInt((intRecordCount - 0.5) /
intRecordsPerPage + 0.5)

'This section creates the hyperlinks for the page numbers in a
string to be posted anywhere on the page any number of times
Dim intPagesAround, intRightPages, intLeftPages
intPagesAround = 2 'Page number hyperlinks on either side of
select page
'Show default page numbers on either side
intRightPages = intPagesAround
intLeftPages = intPagesAround

'Calculates how many page numbers are shown on either side of
the selected page to show a total of intPagesAround * 2 + the
selected page
If intPageNumber <= intPagesAround Then
intRightPages = intPagesAround * 2 - intPageNumber + 1
ElseIf intPageNumber >= intNumberOfPages - intPagesAround Then
intLeftPages = intPagesAround * 2 - intNumberOfPages +
intPageNumber
End If

Dim strPageCount, intPageCount
'If the page number is greater than 1, then a previous button
is added for easy descending
If intPageNumber > 1 Then
strPageCount = strPageCount & "<a
href=""pagenumbers.asp?page=" & intPageNumber - 1 & """>«</a> "
Else
strPageCount = strPageCount & "<span style=""color:
#FFFFFF;"">«</span> "
End If

For intPageCount = 1 To intNumberOfPages
If intPageCount <> intPageNumber Then
If intPageCount <= intPageNumber + intRightPages And
intPageCount >= intPageNumber - intLeftPages Then
strPageCount = strPageCount & "<a
href=""pagenumbers.asp?page=" & intPageCount & """>" &
intPageCount & "</a> "
End If
Else
strPageCount = strPageCount & intPageCount & " "
End If
Next

'If the page number is less than the total amount of pages,
then a next button is added for easy forwarding
If intPageNumber < intNumberOfPages Then
strPageCount = strPageCount & "<a
href=""pagenumbers.asp?page=" & intPageNumber + 1 & """>»</a> "
Else
strPageCount = strPageCount & "<span style=""color:
#FFFFFF;"">«</span> "
End If
'strPageCount is the string that contains all the page number
hyperlinks

End If

'Grab records from database
SQL = "SELECT ID, fldInfo FROM tblData ORDER BY ID DESC"
Set oRS = oConn.Execute(SQL)

'Move cursor to appropriate record depending on page number
oRS.MoveFirst 'Move cursor to first record
If intPageNumber <= intNumberOfPages And intPageNumber >= 1 Then
'Check to make sure page number entered is in correct parameters
oRS.Move((intPageNumber - 1) * intRecordsPerPage) 'Move record
cursor to specific position determined by page number given
ElseIf intPageNumber < 1 Then 'Check to see if the QueryString
page number is less than 1
Response.Redirect("PageNumbers.asp?page=1") 'Reset QueryString
due to user fault
'OR Move first because QueryString page number is less than 1
'oRS.MoveFirst 'Moves the record cursor to the begining of the
records, but does not change user's mistake in the QueryString
ElseIf intPageNumber > intNumberOfPages Then 'Check to see if the
QueryString page number is greater than the number of pages
available
Response.Redirect("PageNumbers.asp?page=" & intNumberOfPages)
'Reset QueryString due to user fault
'OR Move to last page because QueryString page number is
greater than the number of pages available
'oRS.Move((intNumberOfPages - 1) * intRecordsPerPage) 'Moves
the record cursor to the end of the last records shown, but does
not change user's mistake in the QueryString
End If

'Display information about records for testing and trouble
shooting errors
Response.Write "<b>Records Per Page: </b>" & intRecordsPerPage &
"<br />"
Response.Write "<b>Record Count: </b>" & intRecordCount & "<br
/>"
Response.Write "<b>Number Of Pages: </b>" & intNumberOfPages &
"<br />"
Response.Write "<b>Page Number: </b>" & intPageNumber & "<br />"
Response.Write "<br />"
'Display page number hyperlinks
Response.Write strPageCount
Response.Write "<br /><br />"

'Display information in the database depending on selected page
number
Dim intCount
Do Until intCount = intRecordsPerPage Or oRS.EOF
intCount = intCount + 1
Response.Write oRS("ID") & " " & oRS("fldInfo")
Response.Write "<br />"
oRS.MoveNext
Loop
%>

--
Vil du lære at kode HTML, XHTML, CSS, SSI, ASP eller ASP.NET?
- Pædagogiske tutorials på dansk
- Kom godt i gang med koderne
KLIK HER! => http://www.html.dk/tutorials

Andreas Andersen (11-06-2011)
Kommentar
Fra : Andreas Andersen


Dato : 11-06-11 08:25

Den 22-05-2011 16:02, René Jørgensen skrev:
> Hej
>
> Jeg har en brugerliste som efterhånden er lidt for lang til at
> gennemskue på websiden, og vil derfor opsplitte den med paging
> (asp + mysql). Er der nogen som kan anbefale et script eller
> måske har noget kode liggende jeg kan arbejde videre på?

MySQL har LIMIT.

SELECT * FROM MyTable ORDER BY Whatever LIMIT 10, 10

Det returnerer 10 rækker startende fra position 10. Så skal du bare have
sendt et startindeks til dit script myscript?start=120 og sætte nogle
links ind i bunden, der går til passende værdier af start.

--
Andreas

Søg
Reklame
Statistik
Spørgsmål : 177558
Tips : 31968
Nyheder : 719565
Indlæg : 6408925
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste