1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Splitting a string in coldfusion

Discussion in 'Programming' started by keyri88, Apr 22, 2009.

  1. #1
    I'm using a dropdown box to allow the user to rate an attribute one to five.

    <select name = "Rating">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>

    But as there are going to be different things rated I cannot just pass there rating I also need the id of the value they are rating: So I tried this:

    <select name = "Rating">
    <option value="#id#=1">1</option>
    <option value="#id#=2">2</option>
    <option value="#id#=3">3</option>
    <option value="#id#=4">4</option>
    <option value="#id#=5">5</option>
    </select>

    This worked as in it passed both the Id and what its rating was to the next page and using:
    Form.rating#
    it blocks them together like so:
    0=1,5=1,4=1,2=1,8=1,9=1,1=1
    with each first value bieng the id and the second value the rating that id got.
    So to continue with this solution is there a way to split this up? and store them as two seperate variable on the second page. Or is there a better solution overall?

    Any help would be appreciated
    Thanks
    Mike
     
    keyri88, Apr 22, 2009 IP
  2. Kirsten`

    Kirsten` Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    I have done this pairing several times, so I think it's a fine way to do it (though sometimes I over-complicate the code). ;)

    This is how I would split it up ... I haven't tried it, so there maybe some errors with it.

    <cfset list_ids = "">
    <cfset list_ratings = "">

    <cfloop list="#form.rating#" index="i" delimiters=",">

    <cfloop list="#i#" index="j" delimiters="=">

    <cfif ListLen(list_ids) eq ListLen(list_ratings)>
    <cfset list_ids = ListAppend(list_ids, j)>
    <cfelse>
    <cfset list_ratings = ListAppend(list_ratings, j)>
    </cfif>

    </cfloop>

    </cfloop>


    Then you will have two lists, one of the ids and one of the ratings. When you do a ListGetAt for one list at a certain position, you can use the same position for the other list to get the corresponding value.

    Hope that's helpful,
    Kirsten
     
    Kirsten`, Apr 22, 2009 IP
  3. keyri88

    keyri88 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Kirsten,
    That did the trick it seperated it into two lists so I know have:

    #list_ids# giving me:
    0,5,6,4,7,2,3,8,9,1
    and
    #list_ratings#
    1,1,1,1,1,1,1,1,1,1

    Which is brilliant thats my two attributes split up, I just need to get rid of the comma's between the numbers now so I've tried using:

    <cfset theid = replace(#list_ids#,',','')>

    and it does remove the comma but only for the first value in the list.
    05,6,4,7,2,3,8,9,1
    Whats the best way to get it to remove them throughout the whole list?

    Thanks
    Mike
     
    keyri88, Apr 23, 2009 IP
  4. Kirsten`

    Kirsten` Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi Mike,

    Glad that worked well! :) So for the removing of all the commas you need to set your scope in the Replace tag. It's default is 'one' so you need to add 'All' as the last parameter.

    Replace(#list_ids#,',','','All')

    Have a great day!
    Kirsten
     
    Kirsten`, Apr 23, 2009 IP