Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
How do I return only set number of characters from a database field, using .asp?
Old 03-26-2007, 09:47 PM How do I return only set number of characters from a database field, using .asp?
Experienced Talker

Posts: 30
Trades: 0
First up, I wasn't sure whether this belonged here or in the ASP Forum, so my apologies if this is the wrong place.

I'm calling data from a mysql database to show the top 20 links from my Link Dump here - http://www.milkmansworld.co.uk/guestbook/default.asp

I don't want the whole title of the link to show up, just the first 17 characters as it does here using php - http://www.milkmansworld.co.uk

This is the basic code I am using,



<%
Dim sConnection, objConn , objRS

sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=name; UID=login;PASSWORD=password; OPTION=3"

Set objConn = Server.CreateObject("ADODB.Connection")

objConn.Open(sConnection)

Set objRS = objConn.Execute("SELECT * FROM database WHERE database.approved=""yes"" ORDER BY clicks DESC LIMIT 0, 20")




While Not objRS.EOF
Response.Write "<a href=""" & objRS.Fields("url") & """ target=_blank"""">" & objRS.Fields("title") & "</a> <br><p>"




objRS.MoveNext
Wend

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>

Any help will be much appreciated,

Thanks
milkman is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-27-2007, 03:01 AM Re: How do I return only set number of characters from a database field, using .asp?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
dim title as string = objRS.Fields("title")
if len(title) > 17 then title = left(title, 17)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 03-27-2007, 04:29 AM Re: How do I return only set number of characters from a database field, using .asp?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Code:
function GetSnippet(strIn,intMaxLen)
' extract a snippet of text from string
dim temp
dim EndPos
temp = left(strIn,intMaxLen)
EndPos = InStrRev(temp," ")
temp = left(temp, EndPos)
GetSnippet = temp
end function
It cuts off at the end of a word rather than splitting words in the middle

I use it for my "Read More ..." links "
Click here to Read More ...
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-27-2007, 07:41 PM Re: How do I return only set number of characters from a database field, using .asp?
Experienced Talker

Posts: 30
Trades: 0
Thanks for the replies.

I've tried both, but I couldn't get either to work,

With chrishirst's solution I take it I should be using the "display code" half way down this page to display the links on my page - http://www.tek-tips.com/viewthread.cfm?qid=882989,

But I get this error message

Variable is undefined: 'strItem'

on this line

strItem = GetSnippet(objRS.fields("title"),17)



and for ForrestCroce's solution I get the message

Expected end of statement

dim title as string = objRS.Fields("title")
----------^

More than likely i'm not using the code in the right place, or for chrishirst's solution i've substituted the wrong words with my field names.

I think ForrestCroce's solution is the one i'd rather use, I don't want the link title to go over 17 characters regardless and i'm happy for a word to be cut in half as that's how it is on my php pages. Plus ForrestCroce's solution is only 2 lines of code, simpler the better as far as i'm concerned.

Thanks
milkman is offline
Reply With Quote
View Public Profile
 
Old 03-27-2007, 07:56 PM Re: How do I return only set number of characters from a database field, using .asp?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Variable is undefined: 'strItem'
Have you dimensioned the variable (dim strItem) before using it?

Answer is No BTW

Quote:
Expected end of statement

dim title as string = objRS.Fields("title")
----------^
In asp VBScript all variables are variant types until given a value
so it should be

Code:
dim title
title = objRS.Fields("title")
Use whichever code you prefer of course , but the advantage of a function is;
declare it once, use it many times.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-27-2007, 08:11 PM Re: How do I return only set number of characters from a database field, using .asp?
Experienced Talker

Posts: 30
Trades: 0
Thanks Chris, you are of course a legend, I think you've answered every question I've ever asked on this forum.

Have I dimensioned the variable?
If I knew how or even what this is, I may have done.

I tried,

dim title
title = objRS.Fields("title")

as one of my many trial and error attempts, I get this error message though,

Object required: ''

on this line

title = objRS.Fields("title")

Thanks
milkman is offline
Reply With Quote
View Public Profile
 
Old 03-27-2007, 08:22 PM Re: How do I return only set number of characters from a database field, using .asp?
Experienced Talker

Posts: 30
Trades: 0
I just moved these 2 lines down the page

title = objRS.Fields("title")
if len(title) > 17 then title = left(title, 17)

below,

Set objRS = objConn.Execute("SELECT ..................

I now get no error message but the page still displays the whole title and not just 17 characters.
milkman is offline
Reply With Quote
View Public Profile
 
Old 03-27-2007, 08:34 PM Re: How do I return only set number of characters from a database field, using .asp?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
ok to redo your code a bit, 2 alternate lines of display code

Additions in red for my function
Additions in green for Forrest's code
Code:
<%
function GetSnippet(strIn,intMaxLen)
' extract a snippet of text from string
dim temp
dim EndPos
temp = left(strIn,intMaxLen)
EndPos = InStrRev(temp," ")
temp = left(temp, EndPos)
GetSnippet = temp
end function
%>
<% 
Dim sConnection, objConn , objRS 



sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=name; UID=login;PASSWORD=password; OPTION=3" 

Set objConn = Server.CreateObject("ADODB.Connection") 

objConn.Open(sConnection) 

Set objRS = objConn.Execute("SELECT * FROM database WHERE database.approved=""yes"" ORDER BY clicks DESC LIMIT 0, 20")




While Not objRS.EOF
Response.Write "<a href=""" & objRS.Fields("url") & """ target=_blank"""">" & getSnippet(objRS.Fields("title"),17) & "</a> <br><p>"


Response.Write "<a href=""" & objRS.Fields("url") & """ target=_blank"""">" & left(objRS.Fields("title"),17) & "</a> <br><p>"





objRS.MoveNext
Wend 

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>
Usually the function would be in a seperate code library and included into any page that needed to use it, so it can be called whenever you wanted any text cutting down.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-27-2007, 08:41 PM Re: How do I return only set number of characters from a database field, using .asp?
Experienced Talker

Posts: 30
Trades: 0
Nice one, works like a charm, thanks again for that.
milkman is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How do I return only set number of characters from a database field, using .asp?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45776 seconds with 12 queries