Skip to main content
Known Participant
September 10, 2010
Answered

delimited input cfloop

  • September 10, 2010
  • 2 replies
  • 727 views

I am trying to use a list of delimited values and put it in a database. The list is setup as follows:

siteId|SiteName|siteURL|galleryURL|descript|dateAdded|type;

siteId|SiteName|siteURL|galleryURL|descript|dateAdded|type;

siteId|SiteName|siteURL|galleryURL|descript|dateAdded|type;

As you can see the values are delimited by pipe and each one has a semi-colon on the end. I have comprised the follow code that doesn't seem to work. What is wrong? (Assuming its gets passed the form.txtBox1 from a seperate script.

<cfloop index="gallery" list="#form.txtBox1#" delimiters="|">

    <cfquery name="importList">

         INSERT INTO gallery (siteId,siteName,siteUrl,galleryUrl,descript,dateAdded,type)

         VALUES

                  ('#listgetAt('#gallery#',1, '|')#',

                   '#listgetAt('#gallery#',2, '|')#',

                   '#listgetAt('#gallery#',3, '|')#',

   '#listgetAt('#gallery#',4, '|')#',

   '#listgetAt('#gallery#',5, '|')#',

   '#listgetAt('#gallery#',6, '|')#',

   '#listgetAt('#gallery#',7)#'

                  )

   </cfquery>

</cfloop>

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    cfloop delimiters="|"

    At a glance the cfloop delimiter (ie row) should probably be ";"

    '#listgetAt('#gallery#',7)#'

    ... and you are missing the '|' delimiter for the last item.

    If there is a chance the elements might be empty, consider using arrays instead. Unlike most list functions, listToArray() in CF8+ is capable of preserving empty elements. Also, queries executed within a loop are prime candidates for cfqueryparam.

    -Leigh

    2 replies

    Sin0cideAuthor
    Known Participant
    September 10, 2010

    That is correct. I was able to do the task and got the error about the date string. I will have to re-think the date field. Thanks the help guys.

    -__cfSearching__-Correct answer
    Inspiring
    September 10, 2010

    cfloop delimiters="|"

    At a glance the cfloop delimiter (ie row) should probably be ";"

    '#listgetAt('#gallery#',7)#'

    ... and you are missing the '|' delimiter for the last item.

    If there is a chance the elements might be empty, consider using arrays instead. Unlike most list functions, listToArray() in CF8+ is capable of preserving empty elements. Also, queries executed within a loop are prime candidates for cfqueryparam.

    -Leigh

    Inspiring
    September 10, 2010

    In addition to cfSearching's answer, you appear to be storing DateAdded as a string instead of a date.  Bad idea.