Easy Ecommerce at Apollo Hosting

Latest news

This is part 2 and in this part we will show you the scripts to first save the info from the form and then display the latest news items.

First save the info into the database using this script ,

<%
Const adOpenStatic= 3
Const adLockOptimistic = 3
'variables to store title , description , url , author and date items
Dim strTitle , strDescription , strURL , strAuthor , dteToday
strTitle = Request.Form("title")
strDescription = Request.Form("desc")
strURL = Request.Form("url")
strAuthor = Request.Form("author")
dteToday = Time()
Response.Write "strTitle<br>strDescription<br>strURL<br>strAuthor<br>dteToday"
'declare our object variables
Dim objConn , objRS
'create an instance of connection object
Set objConn = Server.CreateObject("ADODB.Connection")
'create an instance of recordset object
Set objRS = Server.Createobject("ADODB.RecordSet")
'open a connection to our database
objConn.Open "DBQ=" & Server.MapPath("latestnews.mdb") & ";Driver={Microsoft Access Driver (*.mdb)}"
'select the fields from the latestnews table
strSQL = "SELECT title ,description,author,submitted,url FROM latestnews"
'open recordset with sql query , connection
objRS.Open strSQL , objConn , adOpenStatic , adLockOptimistic
'add new entry
objRS.AddNew
'fill the fields with the appropriate info
objRS("title") = strTitle
objRS("description") = strDescription
objRS("author") = strAuthor
objRS("submitted") = dteToday
objRS("url") = strURL
'update the database
objRS.Update
'close the recordset
objRS.Close
'destroy object variables
Set objRS = Nothing
Set objConn = Nothing
%>

Now here is the code to display the latest news on the page of your choosing , I chose these colors as examples

<%
'declare constants and object variables
Const adOpenStatic = 3
Const adLockReadOnly = 1
Dim objConn , objRS
'create instances of connection and recordset objects
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.RecordSet")
'open the database
objConn.Open "DBQ=" & Server.MapPath("latestnews.mdb") & ";Driver={Microsoft Access Driver (*.mdb)}"
'select all fields from latestnews table ordering them by submitted
strSQL = " SELECT * FROM latestnews ORDER BY submitted DESC "
'open recordset
objRS.Open strSQL , objConn , adOpenStatic , adLockReadOnly

'start the table html
Response.Write ("<table width ='90%'>")
'loop through 5 records
For i = 1 to 5
if objRS.EOF Then
'no records exit
Exit For
Else
'all of the code below creates an html table
Response.Write "<tr bgcolor='violet'><td>"
'title of news item
Response.Write objRS.Fields("title")
Response.Write "</td><td>"
'author of news item
Response.Write objRS.Fields("author")
Response.Write "</td></tr>"
Response.Write "<tr><td colspan ='2' bgcolor='lavender'>"
'description of news item
Response.Write objRS.Fields("description")
Response.Write "</td></tr>"
Response.Write "<tr bgcolor='violet'><td>"
Response.Write "<a href='"
'display the news item url
Response.Write objRS.Fields("url")
Response.Write "'>"
Response.Write objRS.Fields("url")
Response.Write "</a>"
Response.Write "</td><td>Submitted on :"
'display the submitted date
Response.Write objRS.Fields("submitted")
Response.Write "</td></tr>"
'move to the next record
objRS.MoveNext
end if
Next
Response.Write "</table>"
'close recordset
objRS.Close
'delete object variables
Set objRS = Nothing
Set objConn = Nothing
%>

And here is the output , notice how the entries are ordered by date in this example. In the above code we had a problem at first because unlike MySQL and others Access does not recognise LIMIT , so you cannot limit your returned queries . In this example we jost looped through the recordset 5 times and displayed the newest news items.

The next page contains our download section

Download page

 

Books

cover Professional ASP XML

cover Mastering Active Server Pages 3

cover

Asp Developer's Guide (The Application...