Skip to main content
Inspiring
December 5, 2008
Question

a CFC Guidance Plz?

  • December 5, 2008
  • 9 replies
  • 2270 views
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
This topic has been closed for replies.

9 replies

BKBK
Adobe Expert
December 20, 2008
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">







BKBK
Adobe Expert
December 18, 2008
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.

Inspiring
December 19, 2008
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.
BKBK
Adobe Expert
December 17, 2008
Does this form-check help?

<cfif isDefined("form.ans1")>
<!--- the code in addpoll.cfm --->
<cfset s = structNew()>
...
...
etc. etc.
</cfif>
Inspiring
December 18, 2008
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.
BKBK
Adobe Expert
December 16, 2008
i click add

I don't see where you click. It would help to see all the code.

Inspiring
December 17, 2008
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
BKBK
Adobe Expert
December 14, 2008
The correct order is:

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

Inspiring
December 15, 2008
This Will Create n Number of Loops and How Will i Check that it should get double posted.
BKBK
Adobe Expert
December 15, 2008
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?
BKBK
Adobe Expert
December 7, 2008
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?

Inspiring
December 14, 2008
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>
Inspiring
December 5, 2008
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?
Participating Frequently
December 5, 2008
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
Inspiring
December 5, 2008
Well I want is that above Stuctured to be in a CFC rather than in a CFM Page.

There i am stuck?

Thanks
Inspiring
December 5, 2008
Your question is not clear. Specifically, I don't know what you mean by "convert".