• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I use a variable within paragraphFormat?

Guest
Jan 09, 2014 Jan 09, 2014

Copy link to clipboard

Copied

I would like to insert the firstname of a client within paragrapgFormat(). ie: paragraphFormat(Hi #firstname#, your info......) When I output this the firstname just shows as #firstname#. Is there a way to get his to work?

Views

806

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 10, 2014 Jan 10, 2014

The first instance of 'FirstName' is a variable name. Whereas the last instance is a substring within the string defined by getInfo.message. I am assuming that the message you have saved in the database is:

"Hi"  & FirstName & ",

  We are writing to you to inform you that we received your request to schedule the...

You could modify the code as follows. Give the queries separate names, say, getUser and getMessage. Omit the columns userID and messID from the respective queries. This is because the w

...

Votes

Translate

Translate
Enthusiast ,
Jan 09, 2014 Jan 09, 2014

Copy link to clipboard

Copied

When variables are not processed, chances are you do not have them within a <cfoutput> tag.

<cfoutput>#paragraphFormat( 'Hi ' & firstName & ', your info...' )#</cfoutput>

What we're doing here is concatenating between literal text, like "Hi" and variables that need to be processed (when inside a ColdFusion Built-In Function like ParagraphFormat, you do not need to # the variable name).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2014 Jan 10, 2014

Copy link to clipboard

Copied

Aegis says it all. You could make your code cleaner, hence more maintainable, with a construction like this:

<cfset message = "Hi " & firstName & ", your info...">

<cfoutput>#paragraphFormat(message)#</cfoutput>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 10, 2014 Jan 10, 2014

Copy link to clipboard

Copied

Still not working. Below is my code:

<!--- grabbing firstname --->

<CFQUERY Name="GetInfo" datasource="#application.dsn#">

SELECT userID, Fname, LName, Email, company, phone

FROM users

where UserID = 1

</CFQUERY>

<cfset FirstName = Trim(GetInfo.FName)>

<!--- grabbing message --->

<cfquery name="getInfo" datasource="#application.dsn#">

select messID, name, message

from messages

where messID = 1

</cfquery>

<html>

<head>

    <title>Untitled</title>

</head>

<body>

<table width="300" cellpadding="0" cellspacing="0" border="1">

<tr><td>

<cfoutput query="getInfo">

#paragraphFormat(message)#

</cfoutput>

</td></tr>

</table>

This is the saved message:

"Hi"  & FirstName & ",

  We are writing to you to inform you that we received your request to schedule the...

What am I doing wrong?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2014 Jan 10, 2014

Copy link to clipboard

Copied

The first instance of 'FirstName' is a variable name. Whereas the last instance is a substring within the string defined by getInfo.message. I am assuming that the message you have saved in the database is:

"Hi"  & FirstName & ",

  We are writing to you to inform you that we received your request to schedule the...

You could modify the code as follows. Give the queries separate names, say, getUser and getMessage. Omit the columns userID and messID from the respective queries. This is because the where-clauses already specify the values.

What you could then do is replace the substring "  & FirstName & " in getMessage.message with the variable getUser.FName. The result would be something like

<!--- grabbing firstname --->

<CFQUERY Name="getUser" datasource="#application.dsn#">

SELECT fNname, lName, email, company, phone

FROM users

where userID = 1

</CFQUERY>

<cfset firstNameString = " " & trim(getUser.fName) & " ">

<!--- grabbing message --->

<cfquery name="getMessage" datasource="#application.dsn#">

select name, message

from messages

where messID = 1

</cfquery>

<table width="300" cellpadding="0" cellspacing="0" border="1">

<tr><td>

<cfoutput query="getMessage">

<cfset msg = replaceNoCase(message, '"  & FirstName & "', firstNameString)>

#paragraphFormat(msg)#

</cfoutput>

</td></tr>

</table>

Having said that, there are still 2 things I fail to understand. Firstly, why loop across the query when there is just one row? Secondly, why do you save a static message in the database when you could just save it in the CFML code as a string variable?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 10, 2014 Jan 10, 2014

Copy link to clipboard

Copied

Thanks BKBK,

This code is from a registration form that sends a confirmation email out the registrant. The content is kept in a database so changes can be made by the adminstrator, so they don't have to mess with code. When they register, their name is added to the email. I would like to send the course title and start time for the course as well. From the looks of it, I am better off outputting the individual details before the content .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 10, 2014 Jan 10, 2014

Copy link to clipboard

Copied

I understand. You could store the message, "We are writing to you to inform you that we received your request to schedule the...", in the database. Then add the dynamic part Hi. [first name] by means of code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 12, 2014 Jan 12, 2014

Copy link to clipboard

Copied

Got it to work. After viewing your code, I had an epiphany on how replaceNoCase() works. Can I add multiple replaceNoCase() into my message? I would like to be able to replace courseTitle and Start Date as well.

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 12, 2014 Jan 12, 2014

Copy link to clipboard

Copied

LATEST

Glad to hear you got it to work. Of course, you can replace substrings recursively, like this

<cfset msg = replaceNoCase(message, '"  & FirstName & "', firstNameString)>

<cfset msg = replaceNoCase(msg, substring1, substring2)>

<cfset msg = replaceNoCase(msg, substring3, substring4)>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation