Solved

Count Paragraphs in <cfoutput> of text area data? // Insert image after 1st paragraph?

Posted on 2006-07-05
10
333 Views
Last Modified: 2013-12-24
Is there any way to count paragraphs within the <cfoutput> of a textarea field?
I want to create a page that inserts a div containing another field's data ( photo in this case ) after the first paragraph of an article... .somewhat like this ( pls ignore broken code on sides of page )
http://kanabguide.com/html/offroadtours.shtml

That is the old page, static html.
I want my dynamic output to look like this - one paragraph, then the photo div with dynamic file, caption, alt tag, etc..  then
the rest of the article.


Can it be done?

??

Maybe I have to place the text twice,
trimming everything after the first paragraph the first time,
and trimming off the first paragraph the second time?

My plan is to use this in conjunction with #paragraphFormat#
so maybe I can replace the second <p> in the output with some other code?
0
Comment
Question by:MichaelEvangelista
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 17046184
Where is the data coming from, and what constitutes a paragraph (a line break, two line breaks, etc)?
0
 

Author Comment

by:MichaelEvangelista
ID: 17046240
Hi Chris -

The data is coming from a mySQL database ( text field type ), and the text is being input via a <cftextarea on a simple web 'admin' form.

A double carriage return creates the new paragraph in the form input.
The output, using #paragraphformat(text)#, creates <p> before and </p> after each paragraph

Here is a temporary output page... you can see what I have so far
http://kanabguide.com.wehostwebsites.com/articleDetails.cfm?recordID=54
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 17046265
How about doing something like:

<cfset variables.mypara = paragraphformat(text)>
<cfset variables.mypara = ReplaceNoCase('</p>','</p><img src="someimage.jpg">')>

That will add the image tag after the first paragraph ends.  You can put it inside the <P> by moving the <IMG> tag, and could obviously replace the hard coded src file with a line from the database.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 13

Expert Comment

by:usachrisk1983
ID: 17046273
Sorry, correction to the code:

<cfset variables.mypara = paragraphformat(text)>
<cfset variables.mypara = ReplaceNoCase(variables.mypara,'</p>','</p><img src="someimage.jpg">')>

Also, this assumes you have already gotten the text from the database, which it looks like you can do just fine.
0
 

Author Comment

by:MichaelEvangelista
ID: 17046446
hmmm.... seems like it ought to work, but no joy just yet

I have
<cfset variables.mypara = paragraphformat(recordMainText)>
<cfset variables.mypara = ReplaceNoCase(variables.mypara,'</p>','</p><img src="MREcfCMS/images/full/#recordPhotoFilename#">')>
<p>#variables.mypara#</p>

I am getting the text, but not the image.
http://kanabguide.com.wehostwebsites.com/articleDetails.cfm?recordID=54

Another thing here, I need to insert a div with a few elements, not just the image..
At first, I tried this with the full div code, same results

<cfset variables.mypara = ReplaceNoCase(variables.mypara,'</p>','</p><div class="photowrap"><div align="center"><img src="MREcfCMS/images/full/#recordPhotoFilename#"></div><p class="centered">#recordPhotoTitle#</p><p class="caption">#recordPhotoCaption#</p></div>')>

Seems like it should work, am I missing something?
0
 

Author Comment

by:MichaelEvangelista
ID: 17046681
Someone else suggested listfirst and listrest with the paragraph tag as separator.
Trying that now.
0
 

Author Comment

by:MichaelEvangelista
ID: 17046929
That listfirst code worked!!
(Thanks to ian on the coldfusion forum)

The only thing is that listfirst and listrest both use a single character as a delimiter,
essentially replacing it with a break in the list.
I couldnt use any letters, and <p> wont work becase it is more than on character.

So, I tweaked it a bit to insert a pipe | after the first paragraph
which I know will not be in the text, then use that | character to break the list.

Seems to be working great!

HERE IS A SAMPLE PAGE
http://kanabguide.com.wehostwebsites.com/articleDetails.cfm?recordID=54


HERE IS THE CODE I AM USING
<!--- set up the text output by adding a | after the first paragraph --->
<cfset recordTextFormatted = Replace(#paragraphformat(recordMainText)#, "#chr(13)##chr(10)#", "<p>|")>

<p>#listFirst(recordTextFormatted,"|")#</p>
<div class="photowrap">
 <div align="center"><img src="MREcfCMS/images/full/#recordPhotoFilename#">
 </div>
 <p class="centered">#recordPhotoTitle#</p>
<p class="caption">#recordPhotoCaption#</p>
</div>
<p>#listRest(recordTextFormatted,"|")#</p>
0
 
LVL 13

Accepted Solution

by:
usachrisk1983 earned 125 total points
ID: 17050136
Glad you got it working :)

For what it's worth, chances are that if you looked at the HTML in the original code, it would have inserted the variable as text instead of a variable.  The fix to this would have been:

<cfset variables.mypara = ReplaceNoCase(variables.mypara,'</p>','</p><img src="MREcfCMS/images/full/' & recordPhotoFilename & '">')>
0
 

Author Comment

by:MichaelEvangelista
ID: 17050406
AH thanks. I will mark this page and try again at some point.
Speaking of points... I will accept your answer.
thanks
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 17050855
Thanks, good luck :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question