PDA

View Full Version : Needed some help with these code!


fireburner69
08-30-2002, 01:44 PM
Hello these is the code that i use to make the news section work in my site!
I seperate the news pages to 7 but the problem is that I want to be displayed from the last news to the 1st!
With these code It seperated the pages correct per 7 but the other way around!

Can someone help me out!





<%

Dim nPage, nItems
'Collects Page References
nPage = request("p")



'No Page reference taken then default to first page
if isnumeric(nPage) and nPage <> "" then
if nPage > 0 then
nPage = cint(nPage)
else
nPage = 1
end if
else
nPage = 1
end if

'How many items to display per page
nItems = 7

' Define variants
Dim numID, strTitle, strStory, strLink, strSubmittedBy, strDatestamp, adOpenStatic, adLockReadOnly, adCmdTable, numCounter
adOpenStatic = 1
adLockReadOnly = 1
adCmdTable = 2


' Define database file type and location
Dim objConn, objRS, nRecordCount, nPageCount, nPageSize, sPrev, sNext
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")

objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\athcfs01.hosting.forth\virtual\speftp\db\news.mdb;" & _
"Persist Security Info=False"



' Open News table in database
objRS.Open "News", objConn, adOpenStatic, adLockReadOnly, adCmdTable

' Move to the last record
objRS.MoveLast


objRS.cachesize= 3
objRS.PageSize = nItems

nRecordCount = objRS.RecordCount
nPageCount = objRS.PageCount
nPageSize = objRS.PageSize

if Cint(nPage) <= Cint(nPageCount) then
nPage = nPage
else
nPage = nPageCount
end if

objRS.AbsolutePage = nPage





'Display the data

if not objRS.EOF then
nRecordCount = 0
Do While Not objRS.BOF and nRecordCount < nPageSize

'Do until objRS.eof
response.Write("<p>")
if objRS("Link") <> " " then
response.write("<img src='linkicon.gif'> ")
End If
response.Write("<a href='showstory.asp?storyid=" & objRS("ID") & "'>")
response.Write(objRS("Title") & "</a>")
response.Write("<br><small>" & objRS("Datestamp") & "</small><br><br>")
response.Write("</font></p>")
' objRS.MoveNext

' Do While Not objRS.eof and nRecordCount < nPageSize
nRecordCount = nRecordCount + 1
objRS.MovePrevious
loop




Else
response.write("No Records")
End if

' Create the navi
if Cint(nPage) = Cint(1) or Cint(nPage) = 0 then
sPrev = "<< Prev"
Else
sPrev = "<a href=""table.asp?p=" & (nPage - 1) & """><< Prev</a>"
End if
if Cint(nPage) = Cint(nPageCount) then
sNext = "Next >>"
Else
sNext = "<a href=""table.asp?p=" & (nPage + 1) & """>Next >></a>"
End if


'if not objRS.eof then
' Display the Navi
response.write(sPrev & " | <b>" & nPage & "</b> of " & nPageCount & " | " & sNext)

' Close & clean up
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
%>

raf
08-30-2002, 02:21 PM
did you try :

objRS.MoveFirst
...
objRS.MoveNext

instead of goïng to the last record and then goIng up.
or otherwise sort the data in your table the other way around, or use a select query with something like:

select * from table order by date desc

(where date = variable containing creation-date )

fireburner69
08-30-2002, 02:25 PM
i tried that

but no i noticed about the date


is that correct?

set objRS = objConn.Execute("SELECT * FROM NEWS ORDER BY Date DESC")



can i use something like that?


but where do i put these?

raf
08-30-2002, 02:46 PM
you can't use execute for a select-statement. (execute is for update/insert/delete)

look at code below :
the sql is used as a variable and then read in the rsOrders.Open

date is a variable in the table "klanten". value is inserted when a new record is inserted in the table.

the code then loops through the resulting recordset and builds a table creating a row for each record (last one inserted comes first). backgroundcolours are alternated.


code:

dim conGranIT
set conGranIT=server.CreateObject("adodb.connection")
conGranIT.Open("provider=microsoft.jet.oledb.4.0;data source="&server.MapPath("granit.mdb"))
dim rsKlanten
set rsKlanten= server.CreateObject("adodb.recordset")
dim sql

sql="select * from klanten order by date desc"

rsKlanten.Open sql, conGranIT

if rsKlanten.EOF=true then
response.write("<font color='red'><b>Geen klant gevonden.</b></font><br><br>")
response.write("<font color='red'>Zoektips:<br>&nbsp;&nbsp;- Controleer de ingevoerde gegevens<br>&nbsp;&nbsp;- Probeer met één of meerdere van de andere criteria te zoeken.<br>&nbsp;&nbsp;- Gebruik maar één zoekterm per criterium (zoekterm = naam of deel van een naam)</font>")
response.write("<br><br><a href='javascript:history.back();'>Teruggaan</a>")

else

dim rij
rij = "0"
response.write("<b>Zoekresultaat:</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:history.back();'>Nieuwe selectie</a><br><br>")
response.write("<table border='0' cellpadding='5' cellspacing='0' width='95%'>")
response.write("<tr bgcolor='#c6d8df'>")
response.write("<td><b>Naam(contactpersoon)</b></td><td><b>Firmanaam</b></td><td align='center'><b>Acties</b></td>")
do while rsKlanten.EOF=false 'voor elke rij uit recordset één lijn in tabel. Achtergrondkleur variëren door te kijken naar rest bij deling
if rij MOD 2 = 0 then 'even rijen
response.write("<tr bgcolor='#ddf1f9'>")
else
response.write("<tr bgcolor='#e0ebeb'>")
end if
response.write("<td>" &server.HTMLEncode (rsKlanten.Fields("naam")) &"&nbsp;"& server.HTMLEncode (rsKlanten.Fields("voornaam")) &"</td>")
response.write("<td>" &server.HTMLEncode (rsKlanten.Fields("firmanaam")) &"</td>")
response.write("<td align='right'><a href=order_klant.asp?klantnummer=" &rsKlanten.Fields("klantnummer") & ">Order invoeren </a><br>")
response.write("<a href=gegevens_klant_bekijken.asp?klantnummer=" &rsKlanten.Fields("klantnummer") & ">Klantengegevens bekijken/aanpassen </a></td>")
response.write("</tr>")
rij = rij+1
rsKlanten.MoveNext
loop
response.write("</table>")
end if
rsKlanten.Close
set rsKlanten=nothing

conGranIT.Close
set conGranIT = nothing


Hope it helps you to modify your own code.

whammy
08-31-2002, 12:40 AM
You can use Conn.Execute for a SELECT statement. I do it... so do a lot of other developers at work. ?!?

raf
09-02-2002, 10:26 AM
oops ... my mistake. i learnd it like that buth your right. don't know if one is better then the other.
hope i didn't confuse you fireburner69

glenngv
09-02-2002, 11:02 AM
Originally posted by whammy
You can use Conn.Execute for a SELECT statement. I do it... so do a lot of other developers at work. ?!?

yes you can use that.

the difference is

with:
set rs = conn.execute("Select * from tablename")
rs.recordcount will always return -1.

but with:

Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "Select * from tablename", conn,1,4
rs.recordcount will return the number of records. (depending on the cursor type you use)

whammy
09-02-2002, 01:46 PM
Cool. That's a little tidbit I didn't know yet. ;)