Displaying data from an unknown table

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Displaying data from an unknown table

    I have a simple search I need to implement, but it needs to work for any table.
    I need to display all the data in #form.itemtype#, I have the following
    cfquery that I have written:

    <cfquery datasource=#sysDB# name="results">
    SELECT *
    FROM #FORM.itemType#
    WHERE #FORM.FieldToSearch# LIKE '%#FORM.searchTerm#%'
    </cfquery>

    This works well, as I can see from a cfdump. The problem is I don't know how
    to display the information in an html table, because I don't know any of the
    columns. Can somebody please help? thanks.

    Blazeix Guest

  2. Similar Questions and Discussions

    1. How to take data out of table, restructure the table and then put the data back in
      Hi All Wonder if you could help, I have a bog standard table called STOCKPRICES that has served me well for a while, but now I need to change the...
    2. Problem Displaying Table Information - Please Help
      This problem has to do with .ASP and Access database using both DSN an DSNless connections. I have a bunch of websites hosted with a local host...
    3. [PHP] Displaying MySQL data inside of a table
      Hi Next time you can find a lot of information about this on www.php.net. Check out this example: <?php $conn = mysql_connect("localhost",...
    4. Displaying MySQL data inside of a table
      Hi all, I have this code so far: <? mysql_connect(localhost, USER, PASS); mysql_select_db(DB); $result = mysql_query("SELECT * FROM...
    5. rb_gc_mark(): unknown data type...non object
      I figure I'm getting this because I've got a VALUE that's not a Ruby object. Has anyone figured out a good way to track down such a bug?
  3. #2

    Default Re: Displaying data from an unknown table

    <blockquote>quote:<br><hr><i>Originally posted by: <b><b>Blazeix</b></b></i>
    I have a simple search I need to implement, but it needs to work for any
    table. I need to display all the data in #form.itemtype#, I have the following
    cfquery that I have written:

    <cfquery datasource=#sysDB# name="results">
    SELECT *
    FROM #FORM.itemType#
    WHERE #FORM.FieldToSearch# LIKE '%#FORM.searchTerm#%'
    </cfquery>

    This works well, as I can see from a cfdump. The problem is I don't know how
    to display the information in an html table, because I don't know any of the
    columns. Can somebody please help? thanks.<hr></blockquote>
    Did you try reading the answer to the question you asked about field names?

    Dan Bracuk Guest

  4. #3

    Default Re: Displaying data from an unknown table

    One method.

    <cfquery datasource=#sysDB# name="results">
    SELECT *
    FROM #FORM.itemType#
    WHERE #FORM.FieldToSearch# LIKE '%#FORM.searchTerm#%'
    </cfquery>


    <cfoutput>
    <cfset colHeaderNames = ArrayToList(results.getColumnList())/>
    </cfoutput>
    <table border="1">
    <tr><cfloop list="#colHeaderNames#" index="col" delimiters=",">
    <th align="left" nowrap><cfoutput>#col#</cfoutput></th>
    </cfloop></tr>
    <cfoutput query="results">
    <tr><CFLOOP LIST="#colHeaderNames#" INDEX="col">
    <td align="left" nowrap>#Evaluate(col)#</td></CFLOOP></tr>
    </cfoutput>
    </table>

    Phil

    paross1 Guest

  5. #4

    Default Re: Displaying data from an unknown table

    I'm sorry, I did. I got the fieldnames, but then I can't figure out how to use
    them. I got the first column title off the list, and tried a simple
    #ListFirst(columnlist)#, but it of course just listed the the column name in
    plain text. I have tried various other methods, such as double pound sign, but
    that just listed it in plain text with pound signs around it. I'm sorry I'm
    asking so many questions, I'm rather new to ColdFusion. I've read the
    Coldfusion MX7 manual, but unfortunately I found that it didn't talk to much
    about dynamic sql, there were only a few short pages on it. I don't want to
    really get all my answers from the forum, but if someone would even just point
    me in the right direction or something it would be helpful.

    Blazeix Guest

  6. #5

    Default Re: Displaying data from an unknown table

    Ah. Paross posted as I was typing my reply.
    Thanks, it was the evaluate method that I didn't know about. I can't seem to
    find it in my coldfusion manual, although I just looked it up online at <a
    target=_blank class=ftalternatingbarlinklarge
    href="http://livedocs.macromedia.com.">http://livedocs.macromedia.com.</a> I
    didn't know that website existed prior to just now; I had done everything out
    of the manual. Thanks!

    Blazeix Guest

  7. #6

    Default Re: Displaying data from an unknown table

    <blockquote>quote:<br><hr><i>Originally posted by: <b><b>Blazeix</b></b></i>
    I'm sorry, I did. I got the fieldnames, but then I can't figure out how to use
    them. I got the first column title off the list, and tried a simple
    #ListFirst(columnlist)#, but it of course just listed the the column name in
    plain text. I have tried various other methods, such as double pound sign, but
    that just listed it in plain text with pound signs around it. I'm sorry I'm
    asking so many questions, I'm rather new to ColdFusion. I've read the
    Coldfusion MX7 manual, but unfortunately I found that it didn't talk to much
    about dynamic sql, there were only a few short pages on it. I don't want to
    really get all my answers from the forum, but if someone would even just point
    me in the right direction or something it would be helpful.<hr></blockquote>
    What you appear to be attempting is efficient in terms of getting the most
    bang for the keystroke but it is not necessarily going to result in the most
    user freindly result. For one thing, you are using select *. A well designed
    database will likely have fields that shouldn't be shown to normal people,
    numeric primary key identifiers being a prime example.

    The code you have so far is going to give you the actual column names in
    alphabetical order. Once again, that might not be the best way to display data.

    Finally if you have any date fields, and you don't format them, you are in for
    an unpleasant surprise.

    Another approach is to create string variables based on what you receive from
    the form. These variables will contain your sql.


    Dan Bracuk Guest

  8. #7

    Default Re: Displaying data from an unknown table

    Dan,

    Actually, doing it this way would return the rows in alpha order:

    <table border="1">
    <tr><cfloop list="#results.ColumnList#" index="col" delimiters=",">
    <th align="left" nowrap><cfoutput>#col#</cfoutput></th>
    </cfloop></tr>
    <cfoutput query="results">
    <tr><cfloop list="#results.ColumnList#" index="col">
    <td align="left" nowrap>#results[col][CurrentRow]#</td></cfloop></tr>
    </cfoutput>
    </table>

    Doing it the way that I suggested in my other post should return the columns
    in the same order as the are in the database.

    Phil

    paross1 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139