Skip to main content
Participating Frequently
February 15, 2012
Answered

Looping over a list to create a url string

  • February 15, 2012
  • 1 reply
  • 1680 views

Hi All,

This should be easy for someone with a little experience but for some reason I am having problems with it.  I need to create a url string based on query results.  Basically the query results are:

itemPrice
125542.00
238853.50
125545.00
885442.47

I need to create a url string like www.something.com/index.cfm?item1=122554&item1price=2&item2=23885&item2price=3.50 etc etc (I am actually trying to just create the string and then append it onto a url.  The index needs to be included too like item1, item2 but there will be a different number of results for each order if that makes sense - like this person bought 4 items but somebody else might buy 2).

Can somebody give me a good way to do that?  I have been trying to loop over the query somehow but it isn't working for me.  I ahve also tried making it a list but no dice.  It seems like it should be so simple.

Thanks,

Red

    This topic has been closed for replies.
    Correct answer JR__Bob__Dobbs

    It looks like you are overwriting rather than appending to your string variable.

    Take a looks at the quick sample below.  Note that I have not tested this code.

    <cfset urlString="http://www.example.com/page.cfm?"> <!--- initialize the URL string --->

    <cfloop query="get_cartitems"> <!--- loop over each row and add to the string --->

        <cfif get_cartitems.currentRow gt 1>

            <cfset urlString="#urlString#&"> <!--- add ampersand after first iteration --->

        </cfif>

        <cfset urlString="#urlString#item#get_cartitems.currentRow#=#UrlEncodedFormat(get_cartitems.CIPRRFNBR)#">

        <!--- append price, etc ...  Note use of UrlEncodedFormat function --->

    </cfloop>

    1 reply

    Inspiring
    February 15, 2012

    Please post the code that is not working so the forum users can review it.  It is difficult to see what is going wrong with your code without seeing the code.

    rdaley72Author
    Participating Frequently
    February 15, 2012

    Thanks for replying.  I have tried this a bunch of different ways. 

    This is the way it looks right now but this is after I stripped out a lot of stuff:

    <cfloop query="get_cartitems">

    <cfset theurl = "ID=1521745&ITEM1=#CIPRRFNBR#&AMT1=#CIPRPRC#&TYPE=350403" />

    </cfloop>

    I had it set up like this at one point:

    <cfset itemcount 1>

    <cfloop query="get_cartitems">

    <cfset theurlstring eq theurlstring & "item#itemcount#" & "=" & "#CIPRRFNBR#">

    <cfset itemcount = itemcount + 1>

    </cfloop>

    BKBK
    Community Expert
    Community Expert
    February 15, 2012

    It would also help to know what the column names of the query are, and how they are related to the item number and price.