Solved

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

Posted on 2006-07-05
10
326 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
  • 5
  • 5
10 Comments
 
LVL 13

Expert Comment

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

Author Comment

by:MichaelEvangelista
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:usachrisk1983
Comment Utility
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
Comment Utility
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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

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

Author Comment

by:MichaelEvangelista
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, good luck :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now