Solved

Parsing struct in Coldfusion

Posted on 2013-05-23
4
421 Views
Last Modified: 2013-05-23
Having a major braincramp right now.

Working on a "quick order entry form" that integrates with our ERP system via XML.  Scenario is that a customer searches for a product or group of products.  Results are parsed XML from the ERP system.  Each row of the result has an item#, qty, uom, and price.  The qty is a textfield, uom is a select, price and item# are hidden fields.

Anyway, when I type qty's in a handful of items and submit my form, it contains all items from the results page (which is fine for now).  Some have qty's defined, others are null.

I'm looping params.item to get each individual row:
<cfloop collection="#params.item#" item="objProduct">

Open in new window


Now is where my brain cramp kicks in.  

I need to check each item to make sure qty is not null (and eventually that it is numeric once I get the syntax right).  BUT, I'm having a heck of time referencing the correct variable.  This is what I have now:
<cfif isDefined('params.item[objProduct].qty') and #params.item[objProduct].qty# neq ''>

Open in new window


..And it's giving me
Parameter 1 of function IsDefined, which is now params.item[objProduct].qty, must be a syntactically valid variable name.
 

So, points for:
1. Helping me loop each row and check to make sure qty is defined and is numeric;
2. If qty passes the checks, inserting qty, item, price, and uom to orderlines table (MySQL)

Thanks in advance!
struct-question.png
0
Comment
Question by:Carlos Elgueta
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 500 total points
ID: 39192356
Assuming you have mutliple rows (means it is array of structure), I am posting an example.

<!--- Create a structure and set its contents --->
<cfset departments=Arraynew(1)/>
<cfset departments[1]=Structnew()>
<cfset val=StructInsert(departments[1], "name","John")>
<cfset val=StructInsert(departments[1], "dept","Sales")>
<cfset val=StructInsert(departments[1], "zone","East")>

<cfset departments[2]=Structnew()>
<cfset val=StructInsert(departments[2], "name","Tom")>
<cfset val=StructInsert(departments[2], "dept","Finance")>
<cfset val=StructInsert(departments[2], "zone","East")>

<cfset departments[3]=Structnew()>
<cfset val=StructInsert(departments[3], "name","Mike")>
<cfset val=StructInsert(departments[3], "dept","Education")>
<cfset val=StructInsert(departments[3], "zone","West")>
<cfdump var="#departments#"/>


<!--- Build a table to display the contents --->
<cfoutput>
<table cellpadding="2" cellspacing="2">
  <tr>
  <td><b>Employee</b></td>
  <td><b>Department</b></td>
  <td><b>Zone</b></td>
  </tr>

<!--- In cfloop, use item to create a variable
  called department to hold individual structure  --->
  <cfloop array="#departments#" index="department" >
        <tr>
          <th>#department.name#</th>
          <th>#department.dept#</th>
          <th>#department.zone#</th>
    </tr>
</cfloop>

</table>
</cfoutput>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39192599
The exact syntax depends on the structure of that node - and cfdump doesn't always give an accurate picture.  Can you post the raw xml string for that node/image?

ie    <cfdump var="#toString(params.item)#">
0
 
LVL 1

Author Closing Comment

by:Carlos Elgueta
ID: 39192635
Thanks for the reminder.  This is what I ended up with:

		<cfloop collection="#params.item#" item="objProduct">

			<cfset product = StructNew()>
			<cfset val=StructInsert(product,"orderid",#session.orderid#)>
			<cfset val=StructInsert(product,"qty",#params.item[objProduct].qty#)>
			<cfset val=StructInsert(product,"uom",#params.item[objProduct].uom#)>
			<cfset val=StructInsert(product,"price",#params.item[objProduct].price#)>
			<cfset val=StructInsert(product,"id",#params.item[objProduct].id#)>

			<cfif isDefined('product.qty') and #product.qty# neq ''>

				<cfquery name="insert" datasource="#datasource#">
					insert into orderlines (orderid, item, qty, uom, price) values (
						<cfqueryparam value="#product.orderid#" cfsqltype="cf_sql_integer">,
						<cfqueryparam value="#product.id#" cfsqltype="cf_sql_varchar">,
						<cfqueryparam value="#product.qty#" cfsqltype="cf_sql_integer">,
						<cfqueryparam value="#product.uom#" cfsqltype="cf_sql_varchar">,
						<cfqueryparam value="#product.price#" cfsqltype="cf_sql_varchar">
						)
				</cfquery>

				<cfset productcount = productcount + 1>


			</cfif>


		</cfloop>

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 39192672
Due respect, that seems like a LOT more code than is needed ;-)  (Why are you creating another structure?  You don't need it.)  Also, if the "qty" node doesn't exist, that code will still throw an error.

It should be much simpler. Without seeing the actual string, something like:

<cfloop collection="#params.item#" item="key">
         <cfset entry = params.item[key]>

          <!--- if qty exists and is numeric ... --->
          <cfif structKeyExists(entry, "qty") and isNumeric(entry.qty)>
                  <cfquery name="insert" datasource="#datasource#">
                            INSERT INTO orderlines (..columns...)
                             VALUES (
                                 <cfqueryparam value="#session.orderid#" cfsqltype="cf_sql_integer">,
                                 <cfqueryparam value="#entry.id#" cfsqltype="cf_sql_varchar">,
                                 <cfqueryparam value="#entry.qty#" cfsqltype="cf_sql_integer">,
                                 <cfqueryparam value="#entry.uom#" cfsqltype="cf_sql_varchar">,
                                 <cfqueryparam value="#entry.price#" cfsqltype="cf_sql_varchar">
                  </cfquery>
            </cfif>
      </cfloop>
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what type of join should I use on these 2 queries 9 111
Detect and combat possible robot 7 119
Coldfusion - query to list question 3 56
code conversion assistance required 1 34
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question