8 Replies Latest reply on Jan 12, 2014 6:07 AM by BKBK

    How do I use a variable within paragraphFormat?

    rickaclark54 Level 1

      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?

        • 1. Re: How do I use a variable within paragraphFormat?
          Aegis Kleais Level 3

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

          • 2. Re: How do I use a variable within paragraphFormat?
            BKBK Adobe Community Professional & MVP

            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>

            • 3. Re: How do I use a variable within paragraphFormat?
              rickaclark54 Level 1

              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?

              • 4. Re: How do I use a variable within paragraphFormat?
                BKBK Adobe Community Professional & MVP

                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?

                • 5. Re: How do I use a variable within paragraphFormat?
                  rickaclark54 Level 1

                  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 .

                  • 6. Re: How do I use a variable within paragraphFormat?
                    BKBK Adobe Community Professional & MVP

                    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.

                    • 7. Re: How do I use a variable within paragraphFormat?
                      rickaclark54 Level 1

                      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.

                      • 8. Re: How do I use a variable within paragraphFormat?
                        BKBK Adobe Community Professional & MVP

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