Skip to main content
lovewebdev
Inspiring
December 1, 2011
Answered

Adding multiple db columns to ValueList()?

  • December 1, 2011
  • 2 replies
  • 3027 views

I'm looping over a query inside cfscript. I need to place the results inside of another loop so I'm storing the results of the first query inside of a value list and then displaying that value list inside the second query results.

The only problem is I need to put more than one db column side by side in the value list

My code below...I need to add more columns beside the fname from the getstadd query. Is this even possible?

...

for (

intRow = 1 ;

intRow LTE getstaff.RecordCount ;

intRow = (intRow + 1)

)

{ }

var allstaff = "<span class='staffoncal'>" & ValueList(getstaff.fname, "<br />") & "</span>";

outString = outString & "height='#height#'>" & allstaff & "<p />" & allevents & "</td> ";

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

If you want to save the results to a variable, simply concatenate the values inside your for loop instead.  But there is no need to create extra arrays. Just access the query columns with array notation:

yourVar = "";

for (intRow = 1 ;intRow <= getstaff.RecordCount ;intRow = intRow + 1)

{

    yourVar = yourVar & getStaff.fnames[intRow] & " " & getStaff.lnames[intRow] & "....");

}

2 replies

Inspiring
December 5, 2011

Add the concatonated fields to your query.

lovewebdev
Inspiring
December 6, 2011

Awesome. I think I got it now.

I was having problems because I used getStaff.fname as opposed getStaff.fname[intRow] when concatenated...

[intRow] was correct so what's the difference? I'm kind of new to cfscript and it looks funky. I'm so used to tags...

Inspiring
December 7, 2011

getStaff.fname[intRow] retrieves the value from a specific row in the query. Unless you are within a query loop, getStaff.fname retrieves the value in the first row of the query.

BKBK
Community Expert
Community Expert
December 1, 2011

Valuelist is a function of just one column. As far as I know, you cannot apply it to multiple columns simultaneously.

Anyway, converting from the valuelist to an array will give you more freedom. You will then have individual arrays which you can concatenate, style or display in any way you want. Here follows an example

// array to hold list of first names

var fnames = arrayNew(1);

// array to hold list of corresponding last names

var lnames = arrayNew(1);

// initialize the arrays

var isSet = arraySet(fnames, 1,getstaff.recordCount, "")>

var isSet = arraySet(lnames, 1,getstaff.recordCount, "")>

// set the array entries

fnames = listToArray(valueList(getstaff.fname));

lnames = listToArray(valueList(getstaff.lname));

// concatenate fname and lname into full name and display

for (

intRow = 1 ;

intRow LTE getstaff.RecordCount ;

intRow = (intRow + 1)

)

{

    writeoutput("<span class='staffoncal'>" & fnames[intRow] & " " & lnames[intRow] & "<br /></span>");

}

lovewebdev
Inspiring
December 1, 2011

the problem is I can't really display the loop results right here:

{

    writeoutput("<span class='staffoncal'>" & fnames[intRow] & " " & lnames[intRow] & "<br /></span>");

}

I need to store those looped results in a variable and concatenate it to another variable later in the page. Here particularly, next to or with the allstaff var:

var allstaff = "<span class='staffoncal'>" & ValueList(getstaff.fname, "<br />") & "</span>";

outString = outString & "height='#height#'>" & allstaff & "<p /></td> ";

-__cfSearching__-Correct answer
Inspiring
December 1, 2011

If you want to save the results to a variable, simply concatenate the values inside your for loop instead.  But there is no need to create extra arrays. Just access the query columns with array notation:

yourVar = "";

for (intRow = 1 ;intRow <= getstaff.RecordCount ;intRow = intRow + 1)

{

    yourVar = yourVar & getStaff.fnames[intRow] & " " & getStaff.lnames[intRow] & "....");

}