Skip to main content
January 9, 2014
Answered

How do I use a variable within paragraphFormat?

  • January 9, 2014
  • 2 replies
  • 1082 views

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?

    This topic has been closed for replies.
    Correct answer BKBK

    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?

    2 replies

    BKBK
    Community Expert
    Community Expert
    January 10, 2014

    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>

    January 10, 2014

    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?

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    January 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 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?

    Inspiring
    January 9, 2014

    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).