Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Headings on a table returned from sql

Status
Not open for further replies.

jon24422531

Technical User
Jan 27, 2004
295
GB
Hi guys,

I know the answer to this is probably really simple, but not only am I new to this asp coding, I am not experienced in HTML or web page design either. I can return a recordset very nicely from my MS SQL database (after extensive reading of this forum) but I can not figure out how to put table headings in the resulting table.


<TABLE BORDER='1'>
<td align=left WIDTH='125'><% = rsData("WorkStation") %></td>
<td align=left WIDTH='185'><% = rsData("UserID") %></td>
<td align=left WIDTH='45'><% = rsData("ControllingSite") %></td>
<td align=left WIDTH='170'><% = rsData("Location") %></td>
<td align=left WIDTH='70'><% = rsData("Office")%></td>
</tr></TABLE>

some of you guys are excellent, I am learning such a lot from reading through the forum, many thanks.

Jonathan Schofield
 
Code:
<table>
<tr>
<% 
  for each fieldItem in rs.fields
    response.write "<th>" & fieldItem.name & "</th>"
  next
%>
</tr>

<%
  do while not rs.eof
    response.write "<tr>"
    for each fieldItem in rs.fields
      response.write "<td>&nbsp;" & fieldItem.value & "</td>"
    next
    response.write "</tr>"
    rs.movenext
  loop
%>
</table>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
here you go...
Code:
<table border="1">
<tr>
    <td width="125">WorkStation</td>
    <td width="185">User ID</td>
    <td width="45">Controlling Site</td>
    <td width="170">Location</td>
    <td width="70">Office</td>
</tr>
<tr>
    <td><%=rsData("WorkStation")%></td>
    <td><%=rsData("UserID")%></td>
    <td><%=rsData("ControllingSite")%></td>
    <td><%=rsData("Location")%></td>
    <td><%=rsData("Office")%></td>
</tr>
</table>

Tony
________________________________________________________________________________
 
Many thanks guys. Fantastic & quick response. I tried both, mwolf00's worked the best and returned just what I wanted, I will need to play around a little for some different table formatting. FesterSXS's version put a table heading for each returned recordset. I had tried something similar a little earlier with the same results.

Jonathan Schofield
 
Hi

I would be careful using the database column names for a couple of reasons....

Firstly as with good database design practice the column names may not be formatted/easily readable or describe the data accurately.

Secondly it is never really a good idea to let everybody see what columns you have in a table and could help someone write malicious code to your databse if you don't protect against SQL Injection etc...

just some thoughts..

Thanks


Glen
Conception | Execution
 
With Festers idea you need to move the headers outside of you database loop (similar to mwolfs). If your always going to be selecting the same fields then I would suggest hard coding them like Fester did (see newmediaguy's concerns). Definately keep a copy of mwolf's handy somehwhere though, that little trick of pulling out the field names from the recordset is often overlooked and can be extremely handy :)

-T

barcode_1.gif
 
newmediaguy, if you give the columns aliases in your query, the alias will be displayed and your concerns will be addressed.

SELECT fName + ' ' + lName as "Name", phNum as "Home Phone" FROM myTable

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top