5 Replies - 1656 Views - Last Post: 17 February 2010 - 08:44 AM Rate Topic: -----

#1 Guest_lc123*


Reputation:

Coldfusion page variables question

Posted 14 February 2010 - 09:32 PM

Hello

I have a question about coldfusion variables, and database storage/retrieval

so if, on my page I have a variable #name# being set at the top

<cfset name = "userX">

then on the page, I have text

Hello, <cfoutput>#name#</cfoutput>, Welcome!

which of course in the browser displays as - "Hello, userX, Welcome!"

So say I then decide instead to store this text in a database table.
<cfquery name="retrieve" datasource="test">
select TextToDisplay from TestTable where ID = 1
</cfquery>

Upon retrieval on opening the page, now is displaying

Hello #name# etc.

Is there a way to have this stored in the database and then still pick up that page variable upon being displayed on the page? i apologize if this question makes no sense, or is a silly question. I'm really just learning, and to be honest I didn't even know how to phrase this question for googling purposes.

Thank You
-LC

Is This A Good Question/Topic? 0

Replies To: Coldfusion page variables question

#2 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Coldfusion page variables question

Posted 15 February 2010 - 05:54 AM

Yes but you need to change your code.

You either need to change the query to this:
<cfquery name="retrieve" datasource="test">
select TextToDisplay AS name from TestTable where ID = 1
</cfquery>

Or change the variable on the page to

<cfoutput>#retrieve.textToDisplay#</cfoutput>


This post has been edited by xheartonfire43x: 15 February 2010 - 06:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_lc123*


Reputation:

Re: Coldfusion page variables question

Posted 15 February 2010 - 06:11 PM

View Postxheartonfire43x, on 15 February 2010 - 04:54 AM, said:

Yes but you need to change your code.

You either need to change the query to this:
<cfquery name="retrieve" datasource="test">
select TextToDisplay AS name from TestTable where ID = 1
</cfquery>

Or change the variable on the page to

<cfoutput>#retrieve.textToDisplay#</cfoutput>



I don't understand your response. You say select as name, but the name part is just embedded in the paragraph. I am selecting a paragraph of text, in which there is one variable #name#. So "Hello, #name#, here is some random text"

All this text would be stored in one field in the database then when I pull it back it goes in as html but I want to know if I can have it not display the part #name# as just html text and actually pick up the page variable.

Sorry if I was not clear... or if I am just not understanding your response?
Was This Post Helpful? 0

#4 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Coldfusion page variables question

Posted 17 February 2010 - 07:55 AM

Oh okay I see what you mean. I don't believe so. I know CFML can't be displayed so you couldn't put stuff like <cfinclude template="..." />, but I have never tried with variables. I highly doubt it, but you may be able to find a work around for it. Something like creating in the HTML text a <span id="name"></span> and use jQuery to change the text to your variable. That is best way I could think of.
The code would look something like this:
<cfquery name="retrieve" datasource="test">
    select TextToDisplay from TestTable where ID = 1
</cfquery>
<!--- Text to display needs to have a <span> with an ID of name--->
<html>
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></javascript>
  <cfoutput>
    <script type="text/javascript">
      (document).ready(function(){
        ("##name").text = "#name#";
      })
    </javascript>
  </cfoutput>
</head>
<body>
#retrieve.textToDisplay#



That should work for you.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_lc123*


Reputation:

Re: Coldfusion page variables question

Posted 17 February 2010 - 08:20 AM

xheartonfire - thank you for taking the time to respond again and for the code snippit. To be honest though I would probably not bother going to that extent, it would be easier to just have a bit of text on top of page saying "hello, name" and keep the rest stored in the database. The point is just to allow somebody besides me to edit the text, but they don't need to edit that line ("hello, #name#")

It is good information though that I cannot store variables like that, or CFML in the database. I will know going forward!

thanks again!

-lc
Was This Post Helpful? 0

#6 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Coldfusion page variables question

Posted 17 February 2010 - 08:44 AM

Alright. The only option that I could think of server side is to have the editor use some custom code such as when they type [name] on the code side you parse through the data looking for that and replace it with the variable, but it would be much easier and faster to do it your way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1