Cut the String Length
The function below will return a string with the number of characters
that you set as a parameter.
<%
Function Shorten(sString, sLength)
If Len (sString) > sLength Then
Shorten = Left(sString,sLength) & "...."
Else
Shorten = sString
End If
End Function
%>
Below is sample code that will retrieve the title for each news article
in our database 'News.mdb'. We will use the function Shorten to limit
the length of the display title to 20 characters and add four full
stops. The visitor would see a shortened version of the title that
would act as a teaser. Although our code below doesn't do it you could
hyperlink the title and pass through the ID of the record and then
display the title and news article in full on a separate page.
Example 1:
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Cut the length of the String</title>
</head>
<body>
<%
Function Shorten(sString, sLength)
If Len (sString) > sLength Then
Shorten = Left(sString,sLength) & "...."
Else
Shorten = sString
End If
End Function
Dim oConnection, oRecordset
Dim sSQL, sConnString, sNewsTitle
sSQL="SELECT * FROM tblNews"
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("News.mdb")
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")
oConnection.Open(sConnString)
oRecordset.Open sSQL, oConnection
If oRecordset.EOF Then
Response.Write("There are no titles.")
Else
Do While Not oRecordset.EOF
sNewsTitle= oRecordset("Title")
sNewsTitle=Shorten(sNewsTitle,20)
Response.Write sNewsTitle
Response.Write "<br>"
oRecordset.MoveNext
Loop
End If
oRecordset.Close
oConnection.Close
Set oRecordset = Nothing
Set oConnection = Nothing
%>
</body>
</html>
If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|