Skip to main content
December 3, 2009
Question

A generic CFTable output for all my tables

  • December 3, 2009
  • 2 replies
  • 643 views

I am trying to build a set of pages to maintain my MySQL tables. I do not want to have to create one set for each table, just one set

for all of them. This means that I can never hard code table names or column names.

Here is a standard CFTable example:

<cftable

query="qAuthors"

    border="yes"

    maxrows="8"

    startrow="1"

    colspacing="1"

    colheaders="yes"

    htmltable="yes"

    headerlines="1"

    >

<cfcol header="Author ID" align="left" text="#AuthorID#">

<cfcol header="First Name" align="left" text="#FirstName#">

<cfcol header="Last Name" align="left" text="#LastName#">

</cftable>

Now, the query "qAuthors" can be made generic by using a #URL.Table" parameter passed from an unordered list on

the previous page. Now I need to put the

<cfcol header="Author ID" align="left" text="#AuthorID#">

into a generic CFLoop type iteration. To do this I need to know how many columns there are in the

table supplied by #URL.Table# parameter. I know how to do this by running a query on

column_name from the information_schema.columns table.

What I now need is a way of getting my main query to supply the column name for each row to subsititute

in the "cfcol header" part of the above statement plus the "text=" part. The number of columns variable will

then control how many rows are displayed and show the headers as well.

Any ideas?

Many thanks

    This topic has been closed for replies.

    2 replies

    ilssac
    Inspiring
    December 3, 2009

    Also, have you explored what you can do with the <cfdbinfo...> tag?

    cfdbinfo

    Description

    Lets you retrieve information about a data source, including details about the database, tables, queries, procedures, foreign keys, indexes, and version information about the database, driver, and JDBC.

    http://livedocs.adobe.com/coldfusion/8/htmldocs/Tags_d-e_01.html#3996857

    ilssac
    Inspiring
    December 3, 2009

    You could just use the 'columnList' list that ColdFusion provides with the record set.

    <cfloop list="#qryVariable.columnList#" index="col">

       <cfcol header="#col#" align-"Left" text="#col#">

    </cfloop>

    OR

    You can query your database's system meta tables for a list of column by table name.

    I don't know what table that is in mySQL, but I'm sure Google does.  All relational database have some system table or the other that lists all the column in in all the tables.

    From the first Google result

    SELECT * FROM INFORMATION_SCHEMA.TABLES will
    return metadata information on every table, while


    SELECT * FROM INFORMATION_SCHEMA.COLUMNS will
    return metadata information on every column.

    SO I presume that is going to look something like.

    SELECT aField, bField, cField

    FROM information_schema.tables

    WHERE tableField = 'tableName'

    December 4, 2009

    EUREKA! Its works

    many many thanks