Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / September 2005



Tip: Looking for answers? Try searching our database.

Need HELP PLEASE with CFHTTP

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
murpg - 29 Sep 2005 14:39 GMT
I have inherited some code which works fine for handling one item when the
variable names never change.  However now I need to modify it to handle
multiple items.  For Example, the variable #item_name# always appends to
#item_number1#,
#item_number2#,  etc based on how many items are being returned.  I have the
same situation with the variable #item_name#. Not with standing I have to strip
out multiple + characters using this code. <cfset itemName =
Replace(itemName,"+"," ","ALL")>

Can someone show me how to loop over this cfhttp to handle multiple variables
for Item_name and strip the + characters and how to loop over item_number?

Here is code.

<cfloop list="#cfhttp.FileContent#"
index="curLine"
delimiters="#chr(10)#">
<cfif listGetAt(curLine,1,"=") is "first_name">
<cfset firstName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "last_name">
<cfset lastName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "item_name">
<cfset itemName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "item_number">
<cfset itemName=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "mc_gross">
<cfset mcGross=listGetAt(curLine,2,"=")>
</cfif>
<cfif listGetAt(curLine,1,"=") is "mc_currency">
<cfset mcCurrency=listGetAt(curLine,2,"=")>
</cfif>
</cfloop>
mxstu - 29 Sep 2005 18:21 GMT
Can provide a concrete example of what the cfhttp.FileContent returned looks
like.  Do you know the total number of items you will be handling in each line?
Since I don't know the structure of the data, can you use cfhttp to return the
results in a query? It might be easier than parsing out the values manually.
murpg - 29 Sep 2005 23:27 GMT
Here is the URL to a PDF of the cfdump from cfhttp.
Thanks for helping with this.

http://www.flashwithclass.com/images/products/header.pdf
mxstu - 30 Sep 2005 01:12 GMT
I think I understand the file content. I assume "num_cart_items=2" is the total
number of items in the order?  So for this order you would be looking for:

item_number1
item_number2

... and if you ordered 5 items, then the file content would contain
"num_cart_items=5" and you would be looking for:

item_number1
item_number2
item_number3
item_number4
item_number5

Is this correct?
zoeski80 - 30 Sep 2005 01:36 GMT
See attached code

<!--- copy and paste this all into a .CFM page to see it in action --->

<CFSET FileContent = "mc_gross=0.21
item_number1=e201
tax=0.01
item_number2=e200
payer_id=WE7A73GLF8384
payment_date=05%3A34%3A34+Sep+29%2C+2005+PDT
payment_status=Completed
charset=windows-1252
mc_shipping=0.00
mc_handling=0.00
first_name=George
mc_fee=0.21
custom=
payer_status=verified
business=liszkaetl%40yahoo.com
num_cart_items=2
mc_handling1=0.00
mc_handling2=0.00
payer_email=georgemurphy%40websbygeorge.com
mc_shipping1=0.00
mc_shipping2=0.00
txn_id=606887287Y7396515
payment_type=instant
last_name=Murphy
item_name1=Blue+Marquise+Topaz
receiver_email=liszkaetl%40yahoo.com
item_name2=Rolled+Gold+Curls
payment_fee=0.21
quantity1=1
quantity2=1
receiver_id=AKE7DGRW8MSNC
txn_type=cart
mc_gross_1=0.10
mc_currency=USD
mc_gross_2=0.10
payment_gross=0.21">

<b>OPTION 1</b><BR>
This is how you could modify your current code to loop over each item but you
don't know for how many items to loop over.<BR>
To get rid of the + signs put the Replace statement around each
listGetAt(curLine,2,"=") bit<BR>
<BR>

<cfloop list="#FileContent#" index="curLine" delimiters="#chr(10)#">
    <cfif listGetAt(curLine,1,"=") is "first_name">
        <cfset firstName=listGetAt(curLine,2,"=")>
    </cfif>
    <cfif listGetAt(curLine,1,"=") is "last_name">
        <cfset lastName=listGetAt(curLine,2,"=")>
    </cfif>
    <cfif listGetAt(curLine,1,"=") is "item_name">
        <cfset itemName=listGetAt(curLine,2,"=")>
    </cfif>
    <cfif listGetAt(curLine,1,"=") is "item_number">
        <cfset itemName=listGetAt(curLine,2,"=")>
    </cfif>
    <cfif listGetAt(curLine,1,"=") is "mc_gross">
        <cfset mcGross=listGetAt(curLine,2,"=")>
    </cfif>
    <cfif listGetAt(curLine,1,"=") is "mc_currency">
        <cfset mcCurrency=listGetAt(curLine,2,"=")>
    </cfif>
   
    <!--- item_number --->
    <cfloop from=1 to=2 index='thisNumber'>
        <!--- check if it is item_number#thisNumber# and if so put into variable
called item_number#thisNumber# --->
        <cfif listGetAt(curLine,1,"=") is "item_number#thisNumber#">
            <cfset "item_number#thisNumber#" = listGetAt(curLine,2,"=")>
        </cfif>
    </cfloop>
   
    <!--- item_name --->
    <cfloop from=1 to=2 index='thisNumber'>
        <!--- check if it is item_name#thisNumber# and if so put into variable
called item_name#thisNumber# --->
        <cfif listGetAt(curLine,1,"=") is "item_name#thisNumber#">
            <cfset "item_name#thisNumber#" = listGetAt(curLine,2,"=")>
        </cfif>
    </cfloop>

</cfloop>

<CFOUTPUT>
    item name 1: #item_name1#<BR>
    item number 1: #item_number1#<BR>
    item name 2: #item_name2#<BR>
    item number 2: #item_number2#<BR>
    <BR><BR><BR>
</CFOUTPUT>

<b>OPTION 2</b><BR>
This is the way I would probably go - put all the data into a structure first
so it is easier to handle/manipulate<BR>
This way you can get the number of items from the num_cart_items variable so
you know you need to loop twice only<BR>

<CFSET myFileContent = StructNew()>

<!--- put all items into a structure --->
<cfloop list="#FileContent#" index="curLine" delimiters="#chr(10)#">
    <!--- if there is no value after the = sign then you will get an error if you
try to access item 2
        so check that there are at least 2 items in the list first --->
    <CFIF ListLen(curLine, "=") GTE 2>
        <CFSET temp = StructInsert(myFileContent, listGetAt(curLine,1,"="),
Replace(listGetAt(curLine,2,"="), "+", " ", "ALL"))>
    <CFELSE>
        <CFSET temp = StructInsert(myFileContent, listGetAt(curLine,1,"="), '')>
    </CFIF>

</cfloop>

<CFDUMP VAR="#myFileContent#"><BR><BR>

<!--- put simple values into variables --->
<CFSET firstName = StructFind(myFileContent, "first_name")>
<CFSET lastName = StructFind(myFileContent, "last_name")>

<CFOUTPUT>
    FirstName: #firstName#<BR>
    LastName: #lastName#<BR>
    <BR><BR>
</CFOUTPUT>

<!--- loop through the number of cart items and set variables for them too --->
<CFLOOP FROM="1" TO="#StructFind(myFileContent, "num_cart_items")#"
INDEX="thisItem">
    <CFSET "item_name#thisItem#" = StructFind(myFileContent,
"item_name#thisItem#")>
    <CFSET "item_number#thisItem#" = StructFind(myFileContent,
"item_number#thisItem#")>
    <CFSET "quantity#thisItem#" = StructFind(myFileContent, "quantity#thisItem#")>
    <CFOUTPUT>
        <b>#thisItem#</b><BR>
        item name #thisItem#: #Evaluate('item_name#thisItem#')#<BR>
        item number #thisItem#: #Evaluate('item_number#thisItem#')#<BR>
        item quantity #thisItem#: #Evaluate('quantity#thisItem#')#<BR>
        <BR>
    </CFOUTPUT>
</CFLOOP>
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.