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

a CFC Guidance Plz?

Explorer ,
Dec 04, 2008 Dec 04, 2008

Copy link to clipboard

Copied

Hi Guys I am trying to convert a array query to cfc but i am getting confused..

Please guide me Plz?

<cfset s = structNew()>
<cfset s.ans = arrayNew(1)>
<cfset s.votes = arrayNew(1)>

<cfparam name="s.ans[1]" default="#form.ans1#">
<cfparam name="s.ans[2]" default="#form.ans2#">
<cfparam name="s.ans[3]" default="#form.ans3#">
<cfparam name="s.ans[4]" default="#form.ans4#">

<cfparam name="s.votes[1]" default="0">
<cfparam name="s.votes[2]" default="0">
<cfparam name="s.votes[3]" default="0">
<cfparam name="s.votes[4]" default="0">

<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
Insert Into Answer(QuestionID,answer,votes)
values(#GetQ.QID#,'#s.ans#',#s.votes#)
</cfquery>
</cfloop>


i want to convert the above in cfc
TOPICS
Advanced techniques

Views

1.8K

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
LEGEND ,
Dec 05, 2008 Dec 05, 2008

Copy link to clipboard

Copied

Your question is not clear. Specifically, I don't know what you mean by "convert".

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
Explorer ,
Dec 05, 2008 Dec 05, 2008

Copy link to clipboard

Copied

Well I want is that above Stuctured to be in a CFC rather than in a CFM Page.

There i am stuck?

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
LEGEND ,
Dec 05, 2008 Dec 05, 2008

Copy link to clipboard

Copied

How much do you know about writing cfc's and udfs already. Are you able to:

a. write a function that returns a constant such as "hello world"?
b. write a function that accepts an argument and returns it?
c. put those functions into a cfc and call them from a cfm file?

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
Contributor ,
Dec 05, 2008 Dec 05, 2008

Copy link to clipboard

Copied

Following up on Dan's reply, I think you may be confused as to what a CFC is, and why it would be used. A CFC is a component, and contains functions. You don't want to 'convert' the code, you want to contain it in a CFC.

You need to create a CFC, and a function inside it, with the code you have (above) inside that. There will be input parameters into your function (the form, and datasource values, for example).

You will then call the CFC from a CFM page, using the CFINVOKE tag. There is so much to learn about CFC's that I don't think we could properly respond in just one posting to your question, so I would recommend you read Chapter 10 of the ColdFusion Developer guide (PDF available on the Adobe site).

CFC's are immensely powerful for code re-use, I'm not sure what your programming experience is, but I would also recommend a book titled "Discovering CFCs: ColdFusion MX Components" by Hal Helms.

Write back if you need further assistance.

Cheers,

Davo

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 ,
Dec 07, 2008 Dec 07, 2008

Copy link to clipboard

Copied

A suggestion follows. You could save it as answer.cfc. Then, invoke it on a cfm like this

<!--- cfm page assumed to be in same dir as answer.cfc--->
<cfset ansObj = createobject("component", "answer")>
<cfset answerStatus = ansObj.saveAnswer(form, request.dsn, request.user, request.pass, GetQ.QID)>

My only question is, where do the votes comes from?

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
Explorer ,
Dec 14, 2008 Dec 14, 2008

Copy link to clipboard

Copied

Thanks All.

But while i was playing with Poll Stuff, I noticed my Poll was entering twice.. i made some changes [changes such as moving cfloop outside and inside but both ways the poll is inserted doubling..]

can anyone point me what i am doing wrong?

here is code:

<cfset s = structNew()>
<cfset s.ans = arrayNew(1)>
<cfset s.votes = arrayNew(1)>
<cfparam name="s.ans[1]" default="#form.ans1#">
<cfparam name="s.ans[2]" default="#form.ans2#">
<cfparam name="s.ans[3]" default="#form.ans3#">
<cfparam name="s.ans[4]" default="#form.ans4#">
<cfparam name="s.votes[1]" default="0">
<cfparam name="s.votes[2]" default="0">
<cfparam name="s.votes[3]" default="0">
<cfparam name="s.votes[4]" default="0">
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#" name="check">
<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
SELECT * FROM
Answer
WHERE
QuestionID = #gLastQuestion.QID#
AND
answer = '#s.ans#'
AND
votes = #s.votes#
</cfloop>
</cfquery>
<cfif check.recordcount GT "0">
<cfelse>
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
Insert Into Answer(QuestionID,answer,votes)
values(#gLastQuestion.QID#,'#s.ans#',#s.votes#)
</cfloop>
</cfquery>
</cfif>

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 ,
Dec 14, 2008 Dec 14, 2008

Copy link to clipboard

Copied

The correct order is:

<cfloop>
<cfquery>
</cfquery>
</cfloop>

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
Explorer ,
Dec 14, 2008 Dec 14, 2008

Copy link to clipboard

Copied

This Will Create n Number of Loops and How Will i Check that it should get double posted.

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 ,
Dec 14, 2008 Dec 14, 2008

Copy link to clipboard

Copied

I have looked at your code in some detail. Since you have just one query variable, it will always be overwritten by the loop.

I have tried to second-guess you. I would store the separate queries in an array, and proceed from there. Is that perhaps what you intended to do?

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
Explorer ,
Dec 15, 2008 Dec 15, 2008

Copy link to clipboard

Copied

Thanks but still this code is executing twice. once it gets added, i click add once again and same poll is added again.

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 ,
Dec 15, 2008 Dec 15, 2008

Copy link to clipboard

Copied

i click add

I don't see where you click. It would help to see all the 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
Explorer ,
Dec 16, 2008 Dec 16, 2008

Copy link to clipboard

Copied

Ok Here the Complete Query i am Running the way:

first the add Page:

<cfform name="addpoll">
<table width="100%" border="0">
<tr>
<td colspan="2">
<caption>Add Poll Module Question</caption>
</td>
</tr>
<tr>
<td width="29%">The Question to be Added: </td>
<td width="71%"><cfinput type="text" name="question" id="question" size="50"></td>
</tr>
<tr>
<td>Answer 1: </td>
<td><cfinput name="ans1" type="text" id="ans1"></td>
</tr>
<tr>
<td>Answer 2: </td>
<td><cfinput name="ans2" type="text" id="ans2"></td>
</tr>
<tr>
<td>Answer 3: </td>
<td><cfinput name="ans3" type="text" id="ans3"></td>
</tr>
<tr>
<td>Answer 4: </td>
<td><cfinput name="ans4" type="text" id="ans4"></td>
</tr>
<tr>
<td> </td>
<td><cfinput name="addpollbtn" type="button" id="addpollbtn" value="Add Poll" onClick="doaddPoll()">

now then

doaddPoll = function()
{
ColdFusion.Ajax.submitForm('addpoll', 'addpoll.cfm', myCB, myError);
}

addpoll.cfm

<cfset s = structNew()>
<cfset s.ans = arrayNew(1)>
<cfset s.votes = arrayNew(1)>
<cfparam name="s.ans[1]" default="#form.ans1#">
<cfparam name="s.ans[2]" default="#form.ans2#">
<cfparam name="s.ans[3]" default="#form.ans3#">
<cfparam name="s.ans[4]" default="#form.ans4#">
<cfparam name="s.votes[1]" default="0">
<cfparam name="s.votes[2]" default="0">
<cfparam name="s.votes[3]" default="0">
<cfparam name="s.votes[4]" default="0">
<cfset queryArray=arrayNew(1)>
<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#" name="check">
SELECT * FROM
Answer
WHERE
QuestionID = #gLastQuestion.QID#
AND
answer = '#s.ans#'
AND
votes = #s.votes#
</cfquery>

<cfset queryArray=check>
</cfloop>

<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
<cfif queryArray.recordcount GT 0>
<cfelse>
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
Insert Into Answer(QuestionID,answer,votes)
values(#gLastQuestion.QID#,'#s.ans#',#s.votes#)
</cfquery>
</cfif>
</cfloop>
You added The Poll Successfully
</cfif>

once i click the poll and add it, and i click again the addpoll with same values already in, it posts 2 times.

the code shows all

cheers

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 ,
Dec 17, 2008 Dec 17, 2008

Copy link to clipboard

Copied

Does this form-check help?

<cfif isDefined("form.ans1")>
<!--- the code in addpoll.cfm --->
<cfset s = structNew()>
...
...
etc. etc.
</cfif>

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
Explorer ,
Dec 18, 2008 Dec 18, 2008

Copy link to clipboard

Copied

Well I did something like this:

<cfif Len(Form.question) LTE 2>
Please Enter Question Name Please at least 3 characters.
<cfelseif Len(form.ans1) LTE 1>
You Did not Provide Answer 1 Value
<cfelseif Len(form.ans2) LTE 1>
You Did not Provide Answer 2 Value
<cfelseif Len(form.ans3) LTE 1>
You Did not Provide Answer 3 Value
<cfelseif Len(form.ans4) LTE 1>
You Did not Provide Answer 4 Value
<cfelse>
<cfinvoke component="cfc.poll" method="AddNewQuestion" question="#trim(form.question)#"/>
<cfinvoke component="cfc.poll" method="getLastQuestion" returnvariable="gLastQuestion"/>
<cfset s = structNew()>
<cfset s.ans = arrayNew(1)>
<cfset s.votes = arrayNew(1)>
<cfparam name="s.ans[1]" default="#form.ans1#">
<cfparam name="s.ans[2]" default="#form.ans2#">
<cfparam name="s.ans[3]" default="#form.ans3#">
<cfparam name="s.ans[4]" default="#form.ans4#">
<cfparam name="s.votes[1]" default="0">
<cfparam name="s.votes[2]" default="0">
<cfparam name="s.votes[3]" default="0">
<cfparam name="s.votes[4]" default="0">

but still did not worked.

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
LEGEND ,
Dec 18, 2008 Dec 18, 2008

Copy link to clipboard

Copied

newchickinCF wrote:
> <cfif Len(Form.question) LTE 2>
> Please Enter Question Name Please at least 3 characters.
> <cfelseif Len(form.ans1) LTE 1>
> You Did not Provide Answer 1 Value
> <cfelseif Len(form.ans2) LTE 1>
> You Did not Provide Answer 2 Value
> <cfelseif Len(form.ans3) LTE 1>
> You Did not Provide Answer 3 Value
> <cfelseif Len(form.ans4) LTE 1>
> You Did not Provide Answer 4 Value
> <cfelse>

I have not followed this thread, so I do not know what is trying to be
resolved. But this logic structure has struck me as very odd, and
probably not producing the desired results. At least I can not imagine
what would be a valid requirement for this kind of logic.

<cfif Len(Form.question) LTE 2>
...
<cfelseif Len(form.ans1) LTE 1>
<!--- This is *only* going to be evaluate if the first if clause is
false. I.E. If the Question Name is at least three characters then
Answer One will never be tested. --->
...
<cfelseif Len(form.ans2) LTE 1>
<!--- Again this will *never* be tested if the first or second if
clauses are true. I.E. If the Quesiton name is at least three
characters and Answer 1 is at least one, then Question 2 is not tested. --->
...
<cfelseif Len(form.ans3) LTE 1>
<!--- this continues --->


I don't think you want an if,else if, else structure here. I suspect
you want separate if blocks for each form element to test each one
separately, independent of the results of any other test.

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
Explorer ,
Dec 18, 2008 Dec 18, 2008

Copy link to clipboard

Copied

Here is my Complete Code: That is trivial task making me MAD

<cfif Len(Form.question) LTE 2>
Please Enter Question Name Please at least 3 characters.
<cfelseif Len(form.ans1) LTE 1>
You Did not Provide Answer 1 Value
<cfelseif Len(form.ans2) LTE 1>
You Did not Provide Answer 2 Value
<cfelseif Len(form.ans3) LTE 1>
You Did not Provide Answer 3 Value
<cfelseif Len(form.ans4) LTE 1>
You Did not Provide Answer 4 Value
<cfelse>
<cfinvoke component="cfc.poll" method="AddNewQuestion" question="#trim(form.question)#"/>
<cfinvoke component="cfc.poll" method="getLastQuestion" returnvariable="gLastQuestion"/>
<cfset s = structNew()>
<cfset s.ans = arrayNew(1)>
<cfset s.votes = arrayNew(1)>
<cfparam name="s.ans[1]" default="#form.ans1#">
<cfparam name="s.ans[2]" default="#form.ans2#">
<cfparam name="s.ans[3]" default="#form.ans3#">
<cfparam name="s.ans[4]" default="#form.ans4#">
<cfparam name="s.votes[1]" default="0">
<cfparam name="s.votes[2]" default="0">
<cfparam name="s.votes[3]" default="0">
<cfparam name="s.votes[4]" default="0">
<!---<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#" name="check">
SELECT * FROM
Answer
WHERE
QuestionID = #gLastQuestion.QID#
AND
answer = '#s.ans#'
AND
votes = #s.votes#
</cfquery>
<cfif check.recordcount GT "0">
<cfelse>
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
Insert Into Answer(QuestionID,answer,votes)
values(#gLastQuestion.QID#,'#s.ans#',#s.votes#)
</cfquery>
</cfif>
</cfloop>--->
<cfset queryArray=arrayNew(1)>
<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#" name="check">
SELECT * FROM
Answer
WHERE
QuestionID = #gLastQuestion.QID#
AND
answer = '#s.ans#'
AND
votes = #s.votes#
</cfquery>

<cfset queryArray=check>
</cfloop>

<cfloop from="1" to="#arrayLen(s.ans)#" index="n">
<cfif queryArray.recordcount GT 0>
<cfelse>
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
Insert Into Answer(QuestionID,answer,votes)
values(#gLastQuestion.QID#,'#s.ans#',#s.votes#)
</cfquery>
</cfif>
</cfloop>
You added The Poll Successfully
</cfif>

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
LEGEND ,
Dec 18, 2008 Dec 18, 2008

Copy link to clipboard

Copied

newchickinCF wrote:
> Here is my Complete Code: That is trivial task making me MAD
>

OK I'll bite. So what is the trivial task that is making you mad?

As I said in my first post on this thread I have not been following it
so I do *not* know what trivial task this code is attempting to
accomplish. I notice in your complete code you still have the if|else
if|else structure that, to me, is going to have lots of conditions that
will often not be tested. If that is correct, fine, but it is unusual.

Other then that, I don't have the time nor inclination to do a "desk
run" of your code to analyze what it might be trying to do.

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 ,
Dec 18, 2008 Dec 18, 2008

Copy link to clipboard

Copied

You change your question with every post, making it difficult to keep track. Just showing your code doesn't help much, especially as it is on the longish side. State what your problem is and show as small a piece of code as possible, stating exactly where the problem occurs in the 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
Explorer ,
Dec 18, 2008 Dec 18, 2008

Copy link to clipboard

Copied

Well Guys I have stated a Problem of this Poll Entering 2 times when i add.

ok still i remove the if/else structure. and only works with poll contents. the first time i enter the contents and click add it. add first time, same values are added same. i adds again.

So leaving if/else structure

the problems where i have applied the check.recordcount stuff.

hope this is stuff i was working on.

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 ,
Dec 20, 2008 Dec 20, 2008

Copy link to clipboard

Copied

LATEST
I'm still none the wiser. In any case, are you sure about the line <cfif queryArray.recordcount GT 0>? It tells Coldfusion to do an insert when a query resultset is empty, and to do nothing when the select-query returns results. That seems to me to be the other way round. I expected something like

<cfif queryArray.recordcount GT 0>
<!--- query to insert --->
</cfif>

I also see two more ways of improving the code.

1) Use just one loop. How? Simple. Replace the line <cfset queryArray=check> with the following code block

<!--- Shouldn't this be <cfif check.recordcount GT 0>?--->
<cfif check.recordcount EQ 0>
<cfquery datasource="#request.dsn#" username="#request.user#" password="#request.pass#">
Insert Into Answer(QuestionID,answer,votes)
values(#gLastQuestion.QID#,'#s.ans#',#s.votes#)
</cfquery>
</cfif>


2) Replace this construction

<cfinput name="addpollbtn" type="button" id="addpollbtn" value="Add Poll" onClick="doaddPoll()">

now then

doaddPoll = function()
{
ColdFusion.Ajax.submitForm('addpoll', 'addpoll.cfm', myCB, myError);
}


with this one

<cfform name="addpoll" action="'addpoll.cfm?event=addpoll&cb=#myCB#&err=#myError#">
<cfinput name="addpollbtn" type="submit" id="addpollbtn" value="Add Poll">







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