We help IT Professionals succeed at work.

Help with this code, and function

nmarano
nmarano asked
on
344 Views
Last Modified: 2013-12-24
Experts,

I am fairly new to Cold Fusion, and am looking for some help.  I have this code below that takes a bunch of data and puts it into a comma delimited file.  What happens with the code is it finds let's say studentID 21 and matches all of the information for that student, and creates a row with all of that students info delimited by commas.  This code has been working fine with single answers, but now I have more complex answers.  Trailblazzyr has helped me with a function in order to get the reponses that I am expecting delimited.  The responses that I am now looking at look like this "response~c^correct~1^tasktime~16.98" or "noag^tasktime~7.08"  As I said trailblazzyr helped me with a function to break this string up, but I am having a ton of issues trying to incorporate the function into my existing code.  Any help would be greatly appreciated.

My existing Code..........

<cfset iSet = 2121>
<cfquery name="getItems" datasource="aptvre">
SELECT includedItems
FROM itemset
WHERE itemsetID=#iSet#
</cfquery>
<cfquery name="getParts" datasource="aptvre">
Select re_students.studentID, re_students.firstName, re_students.lastName, takingatest_student.youritemsetstatus
From re_students, takingatest_student
Where re_students.studentID =takingatest_student.studentID
AND takingatest_student.itemsetID =#iSet#
AND re_students.lastName <>'student'
order by re_students.studentID
</cfquery>


<!---need this set to a simple variable for the many loops that use the item list--->
<cfset possibleItems = "#getItems.includedItems#">
<!---make a list of the participantIDs--->
<cfset possibleStudents = ValueList(getParts.studentID,",")>
<cfquery name="getAllAnswers" datasource="aptvre">
SELECT *
FROM itemresponse_student
WHERE itemsetID=#iSet# AND (<cfloop index="itnum" delimiters="," list="#possibleItems#"><cfoutput>itemNumber='#itnum#' OR </cfoutput></cfloop>itemNumber ='SURVEYA000') AND (<cfloop index="studentnum" delimiters="," list="#possibleStudents#">studentID = #studentnum# OR </cfloop> studentID = 90000)
</cfquery>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Student Data 2121</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="../../styles/stylesheet.css">
</link>
</head>
<body>
<cfif getParts.recordcount is not 0>
<div>
<p>sID,LastName,FirstName,youritemsetstatus,
  <cfloop index="i" list="#possibleItems#" delimiters=","><cfoutput>#i#</cfoutput>,</cfloop>!end!<br />
<cfoutput query="getParts">
<cfflush>
<cfquery name="bgData" dbtype="query">
SELECT itemNumber, yourAnswer
FROM getAllAnswers
WHERE studentID = #getParts.studentID#
ORDER BY itemNumber ASC
</cfquery>

#studentID#,#REReplace("#LastName#",",","(comma)","All")#,#FirstName#,#youritemsetstatus#,<cfloop index="i" list="#possibleItems#" delimiters=","><cfloop query="bgData"><cfif #i# IS #bgData.itemNumber#>#REReplace("#yourAnswer#",",","(comma)","All")#<cfbreak></cfif></cfloop>,</cfloop>!end!
<br />
</cfoutput>
</p>
</div>
<cfelse>
<p>
No participant has registered in this course.</p>
</cfif>

</body>
</html>


THE CODE WITH THE FUNCTION TO STRIP THE STRING
*********************************************
<cffunction name="parseResults" returntype="struct" output="false">
 <cfargument name="optionList" type="string" default="">
 <cfscript>
  var myChosenList = arguments.optionList;
  var myListTest = reFindNoCase("([a-z]+)[\^\~]{1}",myChosenList,1,true);
  var myListType = mid(myChosenList,myListTest.pos[2],myListTest.len[2]);
  var responseFind = "";
  var optionFind = "";
  var returnStruct = structNew();
  returnStruct['response'] = "noag";
  returnStruct['correct'] = 0;
  returnStruct['tasktime'] = 0;
  if(myListType neq "noag"){
   if(myListType neq "numoptions"){
    responseFind = reFindNoCase("(response\~){1}([a-z]){1}",myChosenList,1,true);
     returnStruct['response'] = mid(myChosenList,responseFind.pos[3],responseFind.len[3]);
   }else{
    optionFind = reFindNoCase("(option{1}[0-9]+)\~{1}[1]{1}",myChosenList,1,true);
     returnStruct['response'] = mid(myChosenList,optionFind.pos[2],optionFind.len[2]);
   }
   correctMatch = reFindNoCase("(correct\~){1}([0-9]){1}",myChosenList,1,true);
   returnStruct['correct'] = mid(myChosenList,correctMatch.pos[3],correctMatch.len[3]);
  }
  returnStruct['tasktime'] = listLast(listLast(myChosenList,"^"),"~");
 </cfscript>
 <cfreturn returnStruct />
</cffunction>

<cfquery name="Results" datasource="APTVRE">
SELECT Studentid, itemnumber, youranswer
FROM itemresponse_student
WHERE itemsetID='2121'
ORDER BY itemnumber,studentid
</cfquery>

<cfoutput query="Results">
<cfset myRes = parseResults(youranswer)>
<b>StudentID #studentid#</b><br />
<b>#itemnumber#</b><br />
#myRes['response']#<br />
#myRes['correct']#<br />
#myRes['tasktime']#<br /><br />
</cfoutput>

Again any help would be greatly appreciated
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I almost have it working but not quite yet.  It is in fact spitting out the response, but it is not spitting out correct nor am I getting the tasktime.  Here is my code thus far.  If anyone has any suggestions, I would appreacite it

Thanks
nick
<cfset iSet = 2121>
<cfquery name="getItems" datasource="aptvre">
SELECT includedItems
FROM itemset
WHERE itemsetID=#iSet#
</cfquery>
<cfquery name="getParts" datasource="aptvre">
Select re_students.studentID, re_students.firstName, re_students.lastName, takingatest_student.youritemsetstatus
From re_students, takingatest_student
Where re_students.studentID =takingatest_student.studentID
AND takingatest_student.itemsetID =#iSet#
AND re_students.lastName <>'student'
order by re_students.studentID
</cfquery>


<!---need this set to a simple variable for the many loops that use the item list--->
<cfset possibleItems = "#getItems.includedItems#">
<!---make a list of the participantIDs--->
<cfset possibleStudents = ValueList(getParts.studentID,",")>
<!--- Get the answers from the itemresponse_student table --->
<cfquery name="getAllAnswers" datasource="aptvre">
SELECT *
FROM itemresponse_student
WHERE itemsetID=#iSet# AND (<cfloop index="itnum" delimiters="," list="#possibleItems#"><cfoutput>itemNumber='#itnum#' OR </cfoutput></cfloop>itemNumber ='SURVEYA000') AND (<cfloop index="studentnum" delimiters="," list="#possibleStudents#">studentID = #studentnum# OR </cfloop> studentID = 90000)
</cfquery>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Student Data 2121</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="../../styles/stylesheet.css">
</link>
</head>
<body>
<cfif getParts.recordcount is not 0>
<div>
<p>sID,LastName,FirstName,youritemsetstatus,
  <cfloop index="i" list="#possibleItems#" delimiters=","><cfoutput>#i#</cfoutput>,</cfloop>!end!<br />
<cfoutput query="getParts">
<cfflush>
<!---Function to strip out string --->
<cffunction name="parseResults" returntype="struct" output="false">
 <cfargument name="optionList" type="string" default="">
 <cfscript>
  var myChosenList = arguments.optionList;
  var myListTest = reFindNoCase("([a-z]+)[\^\~]{1}",myChosenList,1,true);
  var myListType = mid(myChosenList,myListTest.pos[2],myListTest.len[2]);
  var responseFind = "";
  var optionFind = "";
  var returnStruct = structNew();
  returnStruct['response'] = "noag";
  returnStruct['correct'] = 0;
  returnStruct['tasktime'] = 0;
  if(myListType neq "noag"){
   if(myListType neq "numoptions"){
    responseFind = reFindNoCase("(response\~){1}([a-z]){1}",myChosenList,1,true);
     returnStruct['response'] = mid(myChosenList,responseFind.pos[3],responseFind.len[3]);
   }else{
    optionFind = reFindNoCase("(option{1}[0-9]+)\~{1}[1]{1}",myChosenList,1,true);
     returnStruct['response'] = mid(myChosenList,optionFind.pos[2],optionFind.len[2]);
   }
   correctMatch = reFindNoCase("(correct\~){1}([0-9]){1}",myChosenList,1,true);
   returnStruct['correct'] = mid(myChosenList,correctMatch.pos[3],correctMatch.len[3]);
  }
  returnStruct['tasktime'] = listLast(listLast(myChosenList,"^"),"~");
 </cfscript>
 <cfreturn returnStruct />
</cffunction>
<!--- End of function --->

<cfquery name="bgData" dbtype="query">
SELECT itemNumber, yourAnswer
FROM getAllAnswers
WHERE studentID = #getParts.studentID#
ORDER BY itemNumber ASC
</cfquery>

#studentID#,#REReplace("#LastName#",",","(comma)","All")#,#FirstName#,#youritemsetstatus#,<cfloop index="i" list="#possibleItems#" delimiters=","><cfloop query="bgData"><cfset myRes = parseResults(youranswer)><cfif #i# IS #bgData.itemNumber#>#REReplace("#myRes['response']#","#myRes['correct']#","#myRes['tasktime']#""(comma)","All")#<cfbreak></cfif></cfloop>,</cfloop>!end!
<br />
</cfoutput>
</p>
</div>
<cfelse>
<p>
No participant has registered in this course.</p>
</cfif>

</body>
</html>

Author

Commented:
Sorry trail,

I had made my post before I refreshed and looked at yours

Nick
np... by the way, you don't want to loop over this function or the way I mentioned to create a handle for your function, above you have the function inside your

<cfoutput query="getParts">

you want to make the object first thing on your page, then use it later as a useful function to parse out the lists...

Author

Commented:
Trail,

I did as you suggested, and get this stupid error

Could not find the ColdFusion Component Components.Utils.  
Please check that the given name is correct and that the component exists.  
 
The error occurred in C:\Inetpub\wwwroot\projects\aptefe\researcher\data_itemset2121.cfm: line 2
 
1 : <cfscript>
2 :  utilsClass = createObject("component","Components.Utils");
3 : </cfscript>

 I did as you suggested and created a folder at the root called "Components" and named the function Utils.cfc, but keep getting this error.  I'm posting all of my code, in case I did something wrong which is most likely.


<cfscript>
 utilsClass = createObject("component","Components.Utils");
</cfscript>

<cfset iSet = 2121>
<cfquery name="getItems" datasource="aptvre">
SELECT includedItems
FROM itemset
WHERE itemsetID=#iSet#
</cfquery>
<cfquery name="getParts" datasource="aptvre">
Select re_students.studentID, re_students.firstName, re_students.lastName, takingatest_student.youritemsetstatus
From re_students, takingatest_student
Where re_students.studentID =takingatest_student.studentID
AND takingatest_student.itemsetID =#iSet#
AND re_students.lastName <>'student'
order by re_students.studentID
</cfquery>


<!---need this set to a simple variable for the many loops that use the item list--->
<cfset possibleItems = "#getItems.includedItems#">
<!---make a list of the participantIDs--->
<cfset possibleStudents = ValueList(getParts.studentID,",")>
<!--- Get the answers from the itemresponse_student table --->
<cfquery name="getAllAnswers" datasource="aptvre">
SELECT *
FROM itemresponse_student
WHERE itemsetID=#iSet# AND (<cfloop index="itnum" delimiters="," list="#possibleItems#"><cfoutput>itemNumber='#itnum#' OR </cfoutput></cfloop>itemNumber ='SURVEYA000') AND (<cfloop index="studentnum" delimiters="," list="#possibleStudents#">studentID = #studentnum# OR </cfloop> studentID = 90000)
</cfquery>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Student Data 2121</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="../../styles/stylesheet.css">
</link>
</head>
<body>
<cfif getParts.recordcount is not 0>
<div>
<p>sID,LastName,FirstName,youritemsetstatus,
  <cfloop index="i" list="#possibleItems#" delimiters=","><cfoutput>#i#</cfoutput>,</cfloop>!end!<br />
<cfoutput query="getParts">
<cfflush>

<cfquery name="bgData" dbtype="query">
SELECT itemNumber, yourAnswer
FROM getAllAnswers
WHERE studentID = #getParts.studentID#
ORDER BY itemNumber ASC
</cfquery>

 

#studentID#,#REReplace("#LastName#",",","(comma)","All")#,#FirstName#,#youritemsetstatus#,<cfloop index="i" list="#possibleItems#" delimiters=","><cfloop query="bgData"><cfif #i# IS #bgData.itemNumber#><cfset myRes = utilsClass.parseResults(youranswer)>
  #Studentid# #itemnumber# #myRes['response']# #myRes['correct']# #myRes['tasktime']#<cfbreak></cfif></cfloop>,</cfloop>!end!
<br />
</cfoutput>
</p>
</div>
<cfelse>
<p>
No participant has registered in this course.</p>
</cfif>

</body>
</html>

Author

Commented:
trail,

I know you're busy, but any ideas why I would be getting this error?
Could not find the ColdFusion Component Components.Utils.  
Please check that the given name is correct and that the component exists.  
 
The error occurred in C:\Inetpub\wwwroot\projects\aptefe\researcher\data_itemset2121.cfm: line 2
 
1 : <cfscript>
2 :  utilsClass = createObject("component","Components.Utils");
3 : </cfscript>

 

Author

Commented:
Trail,

I switched

<cfscript>
utilsClass = createObject("component","Components.Utils");
</cfscript>

TO

<cfset utilsClass = createObject("component","Utils")>

It seems to be working.  I'm going to do a few more tests and award you the points.  Thanks for the help and patience

Nick
sorry for the delay, I got swamped ;o)

in your createObject()....

<cfset utilsClass = createObject("component","Utils")>

the second parameter is your path in dot notation...

so if your component you're calling is located

(wwwroot)
 + images
 + Components
   - Utils.cfc
   - Forms.cfc
   - Ecommerce.cfc
   - Etc...
 + Files
 + Etc

under the Components... then you want the path to be Components.Utils for using the Utils class. createObject("component","Components.Utils")

if your CFC is in the same directory as you file using it, then you can reference it directly... createObject("component","Utils")

http://livedocs.macromedia.com/coldfusion/6/Developing_ColdFusion_MX_Applications_with_CFML/buildingComponents2.htm

there's various other ways and a precedence in which coldfusion looks for your CFCs, have a look at the link above for some information on how everthing works with CFC's. They're very useful!


if

<cfset utilsClass = createObject("component","Utils")>

works, then that should do it for ya, if you run into some issues in the future with how you're defining which CFC to use, then the link above should be able to point you in the right direction. It'll give you some background on CFC's and help understand how they're called and used...

Author

Commented:
Thanks for the info

Author

Commented:
trail,

as always thank you for the detailed explainations!

Thanks
Nick
yeah no problem! anytime ;o)

thanks,
-tb55

Author

Commented:
trail,

I have a quick question.  When an answer is returned in this format noag^tasktime~7.08 It is not maintaining the proper itemnumber.  Is it because it's missing the correct in the string as the other string gets returned "response~c^correct~1^tasktime~16.98"?  Any suggestions of anything in the code that is causing this?  Any help would be appreciated

<cfcomponent displayname="Utils">
 <cffunction name="parseResults" returntype="struct" output="false">
 <cfargument name="optionList" type="string" default="">
 <cfscript>
  var myChosenList = arguments.optionList;
  var myListTest = reFindNoCase("([a-z]+)[\^\~]{1}",myChosenList,1,true);
  var myListType = mid(myChosenList,myListTest.pos[2],myListTest.len[2]);
  var responseFind = "";
  var optionFind = "";
  var returnStruct = structNew();
  returnStruct['response'] = "";
  returnStruct['correct'] = "";
  returnStruct['tasktime'] = 0;
  if(myListType neq "noag"){
   if(myListType neq "numoptions"){
    responseFind = reFindNoCase("(response\~){1}([a-z]){1}",myChosenList,1,true);
     returnStruct['response'] = mid(myChosenList,responseFind.pos[3],responseFind.len[3]);
   }else{
    optionFind = reFindNoCase("(option{1}[0-9]+)\~{1}[1]{1}",myChosenList,1,true);
     returnStruct['response'] = mid(myChosenList,optionFind.pos[2],optionFind.len[2]);
   }
   correctMatch = reFindNoCase("(correct\~){1}([0-9]){1}",myChosenList,1,true);
   returnStruct['correct'] = mid(myChosenList,correctMatch.pos[3],correctMatch.len[3]);
  }
  returnStruct['tasktime'] = listLast(listLast(myChosenList,"^"),"~");
 </cfscript>
 <cfreturn returnStruct />
 </cffunction>
</cfcomponent>

Author

Commented:
trail,

is it possible that I am having this issue because not all students answered all of the questions?  Some of the questions are completely blank.  Would that throw everything off?  Any help would be appreciated

Nick

Author

Commented:
If you have time, I posted a follow up

https://www.experts-exchange.com/Web/WebDevSoftware/ColdFusion/Q_22140491.html

Author

Commented:
Trail,

Sorry about all of the follow-up posts to this question.  Below is the output that I am using

#studentID#,#REReplace("#LastName#",",","(comma)","All")#,#FirstName#,#youritemsetstatus#,<cfloop index="i" list="#possibleItems#"delimiters=","><cfloop query="bgData"><cfif #i# IS #bgData.itemNumber#><cfset myRes=utilsClass.parseResults(youranswer)>#myRes['response']#,#myRes['correct']#,#myRes['tasktime']#<cfbreak></cfif></cfloop>,</cfloop>!end!

Is it being thrown off due to this <cfif #i# IS #bgData.itemNumber#> because if #i# IS NOT #bgData.itemNumber#, then the #><cfset myRes=utilsClass.parseResults(youranswer)># doesn't get set?  Again, if you get a sec to look at this, I would greatly appreciate it

Thanks
Nick

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.