Skip to main content
Inspiring
September 19, 2006
Answered

Trying to get first sentence only - code not working

  • September 19, 2006
  • 5 replies
  • 910 views
This is driving me nuts -the code works on one page but not on another and I can't figure out why. The code I'm using to pull out the first sentence from a field is as follows:

<cfset variables.firstsentence = Find( ".", #get_spotlight.description#)>

<cfoutput query="get_spotlight">
<p><#title#<br />
#Left(description, variables.firstsentence)# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>

It's working here: http://www.richland.lib.sc.us/research.cfm

I'm trying to use the same code on another similar page,and the same code doesn't work. It only pulls the first 40 or so characters and I don't know why. I'm assuming it's something to do with the surrounding code, but I can't see what. Can anyone shed some light on this before I go totally nuts?
Here's the page I'm working on: http://www.richland.lib.sc.us/sample/linkstest6.cfm?subjecttype_id=16
And here's all the code (in case something else is causing the problem)

<cfquery name="get_links" datasource="links_1" dbtype="ODBC">
SELECT links.link_id, links.title, links.link, links.description, links.url_home, links.icon, links.url_tutorial, links.discus,
links.subscription, subjecttype.subject_name, subject.link_id, subjecttype.subjecttype_id, subject.subjecttype_id
FROM links, subjecttype, subject
WHERE links.link_id = subject.link_id
AND subjecttype.subjecttype_id = subject.subjecttype_id
AND (subject.subjecttype_id = #url.subjecttype_id#)
ORDER BY title
</cfquery>

<cfset variables.firstsentence = Find( ".", #get_links.description#,0)>

Output (with some extraneous HTML removed - sorry for the lengthy code - I'm sure there's probably a shorter, more elegant way to do this..:

<cfoutput query="get_links">
<cfif subscription IS "Yes">
<cfif icon IS "home">
<cfif url_home IS ""><!--- same url inhouse/remote --->
<p><img src="#icon#4.gif" alt="database icon" width="20" height="20" align="absmiddle"/>
<span class="bold">#TRIM(title)#</span><cfif get_links.discus IS "YES">
<img src="../images/discus_logo.gif" alt="DISCUS" width="61" height="17" align="absmiddle" /></cfif><br />
#Left(description, variables.firstsentence)#<br /><a href="#link#" title="#TRIM(title)# - inside the library">inside the library</a>|
<a href="#link#" title="#TRIM(title)# - home access">home access</a>
|<a href="database_spotlight.cfm?link_id=#link_id#"><img src="moreinfo4.gif" alt="More Information" width="20" height="20" align="absmiddle" /></a>
<cfif url_tutorial IS NOT "">|
<a href="../../pdf/#url_tutorial#" ><img src="tutorial4.gif" alt="Tutorial" width="20" height="20" align="absmiddle" /></a>
</cfif></p>

<cfelse><!--- show two urls inhouse/remote --->
<p><img src="#icon#4.gif" alt="database icon" width="20" height="20" align="absmiddle"/>
<span class="bold">#TRIM(title)#</span><cfif get_links.discus IS "YES">
<img src="../images/discus_logo.gif" alt="DISCUS" width="61" height="17" align="absmiddle" /></cfif><br />
#Left(description, variables.firstsentence)# <br /><a href="#link#" title="#TRIM(title)# - inside the library"> inside the library</a>|
<a href="#url_home#" title="#TRIM(title)# - home access">home access</a> |<a href="database_spotlight.cfm?link_id=#link_id#"><img src="moreinfo4.gif" alt="More Information" width="20" height="20" align="absmiddle" /></a>

<cfif url_tutorial IS NOT "">|
<a href="../../pdf/#url_tutorial#" ><img src="tutorial4.gif" alt="Tutorial" width="20" height="20" align="absmiddle" /></a>
</cfif>
</p>
</cfif>
<cfelseif icon is "inhouse"><!--- show inhouse url --->
<p><img src="#icon#4.gif" alt="database icon" align="absmiddle"/>
<span class="bold">#TRIM(title)#</span><br />
#Left(description, variables.firstsentence)# <br /><a href="#link#" title="#TRIM(title)# - inside the library">inside the library</a> | <a href="database_spotlight.cfm?link_id=#link_id#"><img src="moreinfo4.gif" alt="More Information" width="20" height="20" align="absmiddle" /></a>

<cfif url_tutorial IS NOT "">|
<a href="../../pdf/#url_tutorial#" ><img src="tutorial4.gif" alt="Tutorial" width="20" height="20" align="absmiddle" /></a>
</cfif></p>
</cfif>
</cfif>
</cfoutput>
<p> </p>
<h3>Librarian - Selected Web Sites</h3>
<cfoutput query="get_links"> <!--- non-subscription sites--->
<cfif subscription IS "No">
<p><img src="#icon#4.gif" alt="database icon" align="absmiddle"/>
<a href="#link#" title="#TRIM(title)#">#TRIM(title)#</a><br />
#Left(description, variables.firstsentence)# <br />
<a href="database_spotlight.cfm?link_id=#link_id#"><img src="moreinfo4.gif" alt="More Information" width="20" height="20" align="absmiddle" /></a></p>
</cfif>
</cfoutput>

Thanks for reading this far, and for any assistance or suggestions.
This topic has been closed for replies.
Correct answer Micheller
YES!! That was it - woohoo! Thank you so much. That explains, also, why the same piece of code DID work on another page - on that page I was only pulling out one record instead of many. I looked up the ListFirst function and I understand this - but I never would have figured it out myself.

Thanks much for coming up with the answer - and thanks to everyone who took the time to try -I really appreciate it.

quote:

Originally posted by: Swift
Hi Michelle,

If I'm right, then this is a simple problem. The simple ones are good. :)

This is what you have:
<cfset variables.firstsentence = Find( ".", #get_spotlight.description#)>

<cfoutput query="get_spotlight">
<p><#title#<br />
#Left(description, variables.firstsentence)# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>


What you have done is to base the position of the period off of the first database record, and then applied it to all other database records. Therefore, you are cutting off at position 40 every time, because you'll find that the first record in the database should be cut off at position 40. Instead, you simply have to calculate the position every time inside the loop. Plus, let's just go ahead and make it much easier still by using ListFirst...

<cfoutput query="get_spotlight">
<p><#title#<br />
#ListFirst(Description, ".")# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>

That should do it. Incidentally, if you actually want the period to appear (which I imagine you do), then simply use:

#ListFirst(Description, ".")#.

instead of


#ListFirst(Description, ".")#

HTH,
Swift



5 replies

MichellerAuthor
Inspiring
September 19, 2006
Nope, that didn't make any difference at all.
Inspiring
September 19, 2006
Hi Michelle,

If I'm right, then this is a simple problem. The simple ones are good. :)

This is what you have:
<cfset variables.firstsentence = Find( ".", #get_spotlight.description#)>

<cfoutput query="get_spotlight">
<p><#title#<br />
#Left(description, variables.firstsentence)# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>


What you have done is to base the position of the period off of the first database record, and then applied it to all other database records. Therefore, you are cutting off at position 40 every time, because you'll find that the first record in the database should be cut off at position 40. Instead, you simply have to calculate the position every time inside the loop. Plus, let's just go ahead and make it much easier still by using ListFirst...

<cfoutput query="get_spotlight">
<p><#title#<br />
#ListFirst(Description, ".")# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>

That should do it. Incidentally, if you actually want the period to appear (which I imagine you do), then simply use:

#ListFirst(Description, ".")#.

instead of

#ListFirst(Description, ".")#

HTH,
Swift
MichellerAuthorCorrect answer
Inspiring
September 20, 2006
YES!! That was it - woohoo! Thank you so much. That explains, also, why the same piece of code DID work on another page - on that page I was only pulling out one record instead of many. I looked up the ListFirst function and I understand this - but I never would have figured it out myself.

Thanks much for coming up with the answer - and thanks to everyone who took the time to try -I really appreciate it.

quote:

Originally posted by: Swift
Hi Michelle,

If I'm right, then this is a simple problem. The simple ones are good. :)

This is what you have:
<cfset variables.firstsentence = Find( ".", #get_spotlight.description#)>

<cfoutput query="get_spotlight">
<p><#title#<br />
#Left(description, variables.firstsentence)# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>


What you have done is to base the position of the period off of the first database record, and then applied it to all other database records. Therefore, you are cutting off at position 40 every time, because you'll find that the first record in the database should be cut off at position 40. Instead, you simply have to calculate the position every time inside the loop. Plus, let's just go ahead and make it much easier still by using ListFirst...

<cfoutput query="get_spotlight">
<p><#title#<br />
#ListFirst(Description, ".")# (<a href="database_spotlight.cfm?link_id=#link_id#">find out more...</a>)</p></cfoutput>

That should do it. Incidentally, if you actually want the period to appear (which I imagine you do), then simply use:

#ListFirst(Description, ".")#.

instead of


#ListFirst(Description, ".")#

HTH,
Swift



September 19, 2006
Try leaving off the ",0" in the statement <cfset variables.firstsentence = Find( ".", #get_links.description#,0)>

The default is 1; the zero may be giving you unpredictable results.

Edit:
In ColdFusion V5, I ran a sample Find( ) with the starting character position equal to zero. It threw the following error:
Error Diagnostic Information

An error occurred while evaluating the expression:

found = Find(".", foo, 0)

Error near line 10, column 7.
--------------------------------------------------------------------------------

Parameter 3 of function Find which is now "0" must be a positive integer

MichellerAuthor
Inspiring
September 19, 2006
OK, I realized I was pointing to the wrong query, and when I corrected it, everything started truncating back to about 40 characters again. I do not understand this. What am I doing wrong? I'm again using the code from my first post.
MichellerAuthor
Inspiring
September 19, 2006
Well, at first it was truncating at around 40 characters - I'm not sure what I did that changed it so that it's now truncating at about 160 - also, I just noticed that the first 3 items are fine (and the first 2 in the second '"selected by librarians" list) and then it starts cutting off. At first, I also thought I was telling it somewhere to cut off after so many characters, but I can't see anywhere in the code where that's happening. I'm at a loss to figure out (a) why it's doing this and (b) what I did that made it suddenly start showing more than the first 40 characters.
Inspiring
September 19, 2006
Are the strings the same? Does the 2nd string have an abbreviation like Dr., or Mr, or etc.?
MichellerAuthor
Inspiring
September 19, 2006
Yes - depending on what you mean. There are over 50 records, so of course it's not exactly the same -each record has a different description. BUT - if you look at the example ( http://www.richland.lib.sc.us/sample/linkstest6.cfm?subjecttype_id=16), you can see that every record is getting cut-off after about 40 or so characters - sometimes in the middle of a word. Additionally, if you look at the "Database Spotlight" the page where the code is working ( http://www.richland.lib.sc.us/research.cfm) -you see the entire first sentence for EbscoHost. But on the page where it isn't working -the same sentence for Ebscohost is cut-off - and that one IS exactly the same - same database, same table, same record, same field.
September 19, 2006
It seems to be truncating at 159 characters. Does this number (or 160) appear anywhere in your application?