Link to home
Start Free TrialLog in
Avatar of Carlos Elgueta
Carlos ElguetaFlag for United States of America

asked on

Parsing struct in Coldfusion

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
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)#">
Avatar of Carlos Elgueta

ASKER

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

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>