Skip to main content
Participant
September 11, 2009
Question

how to line up results into columns?

  • September 11, 2009
  • 2 replies
  • 1152 views

I'm attempting to output a query as an Excel-like table, where columns 1,2 are each a column in the query but columns 3 through an unknown number (about 30) are results from a single column of the query. I need each of the columns 4-30 to line up similar data, even if it means a blank cell. Any ideas on how to force this to happen? I've tried various schemes of grouping, looping, etc and nothing lines up the columns.

Example of output:

col 1: person's name
col 2: person's place
col 3: car type
col 4: car type
col 5: car type
col 6: car type
name1place1volvolexusbmw
name2place2toyotalexus
name3place3volvolexusbmw
name4place4toyotabmw

I want similar data values to line up visually. I can do groups to get col 1 and 2, but grouping for cols 3+ leaves out the blank cells, so that the values don't line up.

Any ideas anyone?

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    September 11, 2009

    What about the align attribute of TD? Something like this

    <table border="1">
    <tr><th>col 1</th><th>col 2</th><th>col 3</th>... and so on</tr>
    <cfoutput query="myQuery">
    <tr><td align="left">#col1[currentrow]#</td><td align="left">#col2[currentrow]#</td><td align="left">#col3[currentrow]#</td>... and so on</tr>
    </cfoutput>
    </table>

    Inspiring
    September 12, 2009

    <tr><th>col 1</th><th>col 2</th><th>col 3</th>... and so on</tr>

    But it's the "and so on" bit that is the question here!

    --

    Adam

    Inspiring
    September 11, 2009

    OK, so looking @ your example table, it's less that cols 3+ are "car type", and more tha the third one is "volvo", the fourth is "toyota" etc.  Do you have a predefined list of car types, or are you at the whim of what ends up in the column from the DB?

    If you know the list of possible cars, it's just a matter of looping over that list to create a column for each, and then check the relevant query cell for the presence of that value when rendering each TD for that column, and if it's there: say so; otherwise, leave it blank.

    If you don't know the possible car varieties, then loop over the query first to build a list of possible cars: loop over the query, and for each row, loop of the list of cars and add each unique car name to a list of cars.  Then use that latter list to determine which columns are needed for the actual table, and do the loop over the query again to generate the table, as per above.

    Of course the best thing to do here would be to normalise your data, so you don't have multiple values in a single DB field.  You might not be in a position to rejig your DB schema, but if you are: consider doing this.

    --

    Adam

    Sheryl2Author
    Participant
    September 11, 2009

    Thanks; this got me close. I did as you suggested, except using an array instead of a list.

    We don't know ahead which car variety will be in the column, so I had to create an array and loop over that. Looping over a list caused blank columns, but looping over the array did not.

    Inspiring
    September 11, 2009
    Thanks; this got me close. I did as you suggested, except using an array instead of a list.

    We don't know ahead which car variety will be in the column, so I had to create an array and loop over that. Looping over a list caused blank columns, but looping over the array did not.

    Yeah, an array is probably a better solution anyhow.  I only mentioned lists because a lot of CF developers seem to understand them better than arrays, for some reason.  Seems you don't have that problem ;-)

    --

    Adam