Skip to main content
March 2, 2007
Question

building tables from multiple columns in db

  • March 2, 2007
  • 2 replies
  • 266 views
I'll asked this question several months back and i still cant figure out how to do this... or what is best practices for doing this.

im building a product page and i have table header column data in the database..with headings column1, column2 ...

and table infomation table

is it best to have your data in one column comma delimited? or is there aa way to query and get the information for that product table heder and table info and output in template

the thing is because the table column very from product to product ... the template needs to loop according to how many columns of information there is.

what is best practices for table data in database and outputing columns to cfm template

this kind of works..but returns a error: Invalid CFML construct found on line 1 at column 6.

<cfquery datasource="info" name="tableheader">
Select * from tableheaders
WHERE tableheadersID = 35
</cfquery>

</head>

<body>
<table border="1">
<tr>
<cfloop list="#tableheader.column1#" index="column">
<cfoutput><th>#column#</th></cfoutput>
</cfloop>
</tr>
<cfoutput query="tableheader">
<tr>
<cfloop list="#tableheader.column1#" index="column">
<td>#Evaluate(column)#</td>
</cfloop>
</tr>
</cfoutput>
</table

also..CF livedocs.recommends not using the evaluate because of its overhaed

so in a nutshell.how do you retrive/ output multiple columns for a particular product ID

Thanks for any help!


    This topic has been closed for replies.

    2 replies

    Inspiring
    March 3, 2007
    quote:

    Originally posted by: dennisquery
    I'll asked this question several months back and i still cant figure out how to do this... or what is best practices for doing this.

    im building a product page and i have table header column data in the database..with headings column1, column2 ...
    and table infomation table

    is it best to have your data in one column comma delimited? or is there aa way to query and get the information for that product table heder and table info and output in template


    having your data in one column comma delimited
    is bad.
    It has no redeeming qualities.
    She's a Must to Avoid..
    I recommend against it.
    There are better options out there.
    Participating Frequently
    March 2, 2007
    Code is a little confusing where you're looping a list caled tableheader, and loopqing a query called tableheader.

    Why not just loop the columnList of the query and reference the column data using an array syntax? The below example will product the column names as uppercase and have the same name as the query. For this I'd consider passing each column heading to a udf function such as CapFirst or even implement a localization routine to support table headers in different languages.

    (for this example i'm going to use qryProducts as the query)

    <table>
    <tr>
    <cfloop list="#qryProducts.columnList#" index="column">
    <cfoutput><th>#column#</th></cfoutput>
    </cfloop>
    </tr>
    <cfoutput query="qryProducts">
    <tr>
    <cfloop list="#qryProducts.columnList#" index="column">
    <td>#qryProducts[column][qryProducts.currentRow]#</td>
    </cfloop>
    </tr>
    </cfoutput>
    </table>
    March 3, 2007
    hey thanks alot... that seems to work!

    the only problem im having now.. is to only select the columns that have data in them... i cant figure out the NOT NULL way of writing the query

    when i select all 20 columns and there is only data in 3 .. i get extra spacing