20 Replies Latest reply on Dec 20, 2008 4:10 AM by BKBK

    a CFC Guidance Plz?

    newchickinCF Level 1
      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[n]#',#s.votes[n]#)
      </cfquery>
      </cfloop>


      i want to convert the above in cfc
        • 1. Re: a CFC Guidance Plz?
          Dan Bracuk Level 5
          Your question is not clear. Specifically, I don't know what you mean by "convert".
          • 2. Re: a CFC Guidance Plz?
            newchickinCF Level 1
            Well I want is that above Stuctured to be in a CFC rather than in a CFM Page.

            There i am stuck?

            Thanks
            • 3. Re: a CFC Guidance Plz?
              Dan Bracuk Level 5
              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?
              • 4. Re: a CFC Guidance Plz?
                davidmedifit Level 1
                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
                • 5. Re: a CFC Guidance Plz?
                  BKBK Adobe Community Professional & MVP
                  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?

                  • 6. Re: a CFC Guidance Plz?
                    newchickinCF Level 1
                    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[n]#'
                    AND
                    votes = #s.votes[n]#
                    </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[n]#',#s.votes[n]#)
                    </cfloop>
                    </cfquery>
                    </cfif>
                    • 7. Re: a CFC Guidance Plz?
                      BKBK Adobe Community Professional & MVP
                      The correct order is:

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

                      • 8. Re: a CFC Guidance Plz?
                        newchickinCF Level 1
                        This Will Create n Number of Loops and How Will i Check that it should get double posted.
                        • 9. Re: a CFC Guidance Plz?
                          BKBK Adobe Community Professional & MVP
                          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?
                          • 10. Re: a CFC Guidance Plz?
                            newchickinCF Level 1
                            Thanks but still this code is executing twice. once it gets added, i click add once again and same poll is added again.

                            • 11. Re: a CFC Guidance Plz?
                              BKBK Adobe Community Professional & MVP
                              i click add

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

                              • 12. Re: a CFC Guidance Plz?
                                newchickinCF Level 1
                                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[n]#'
                                AND
                                votes = #s.votes[n]#
                                </cfquery>

                                <cfset queryArray[n]=check>
                                </cfloop>

                                <cfloop from="1" to="#arrayLen(s.ans)#" index="n">
                                <cfif queryArray[n].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[n]#',#s.votes[n]#)
                                </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
                                • 13. Re: a CFC Guidance Plz?
                                  BKBK Adobe Community Professional & MVP
                                  Does this form-check help?

                                  <cfif isDefined("form.ans1")>
                                  <!--- the code in addpoll.cfm --->
                                  <cfset s = structNew()>
                                  ...
                                  ...
                                  etc. etc.
                                  </cfif>
                                  • 14. Re: a CFC Guidance Plz?
                                    newchickinCF Level 1
                                    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.
                                    • 15. Re: a CFC Guidance Plz?
                                      Level 7
                                      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.
                                      • 16. Re: a CFC Guidance Plz?
                                        newchickinCF Level 1
                                        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[n]#'
                                        AND
                                        votes = #s.votes[n]#
                                        </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[n]#',#s.votes[n]#)
                                        </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[n]#'
                                        AND
                                        votes = #s.votes[n]#
                                        </cfquery>

                                        <cfset queryArray[n]=check>
                                        </cfloop>

                                        <cfloop from="1" to="#arrayLen(s.ans)#" index="n">
                                        <cfif queryArray[n].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[n]#',#s.votes[n]#)
                                        </cfquery>
                                        </cfif>
                                        </cfloop>
                                        You added The Poll Successfully
                                        </cfif>
                                        • 17. Re: a CFC Guidance Plz?
                                          Level 7
                                          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.
                                          • 18. Re: a CFC Guidance Plz?
                                            BKBK Adobe Community Professional & MVP
                                            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.

                                            • 19. Re: a CFC Guidance Plz?
                                              newchickinCF Level 1
                                              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.
                                              • 20. Re: a CFC Guidance Plz?
                                                BKBK Adobe Community Professional & MVP
                                                I'm still none the wiser. In any case, are you sure about the line <cfif queryArray[n].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[n].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[n]=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[n]#',#s.votes[n]#)
                                                </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">