Cut the String Length

The function below will return a string with the number of characters that you set as a parameter.

<%
'Our function Shorten can accept 2 parameters
'The string and the length of characters to shorten the string too
Function Shorten(sString, sLength)
'Make sure length of the string is more than the number of characters to return
If Len (sString) > sLength Then
'Add four full stops so that the visitor knows the string has been abbreviated
         Shorten = Left(sString,sLength) & "...."
Else
'If the string is shorter than the number of characters allowed then don't shorten
         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
'Declare your variables
Dim oConnection, oRecordset
Dim sSQL, sConnString, sNewsTitle

'declare SQL statement that will query the database
sSQL="SELECT * FROM tblNews"

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("News.mdb")


'create an ADO connection and recordset
Set oConnection = Server.CreateObject("ADODB.Connection")
Set oRecordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
oConnection.Open(sConnString)

'Open the recordset object, execute the SQL statement
oRecordset.Open sSQL, oConnection

'first of all determine whether there are any records
If oRecordset.EOF Then
Response.Write("There are no titles.")
Else
'If there are records then loop through the fields
Do While Not oRecordset.EOF
sNewsTitle= oRecordset("Title")
'Call our function Shorten
'pass in our variable 'sNewsTitle' and assign returned value to variable
sNewsTitle=Shorten(sNewsTitle,20)
Response.Write sNewsTitle
Response.Write "<br>"  'insert a line break
'move on to the next record
oRecordset.MoveNext
Loop
End If

'close the connection and recordset objects and free up resources
oRecordset.Close
oConnection.Close
Set oRecordset = Nothing
Set oConnection = Nothing
%>

</body>
</html>

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.