|
Data to HTML |
miran
Member #2,407
June 2002
|
This is not Allegro related, but maybe someone can help me anyway. I have some data in a database and I need to display it in a web page. I'm using .NET. What are my options? There's an existing solution that does it ASP style - that is it reads data from the database and writes HTML as output. What other options are there? One way I can think of is reading the data into XML and then using XSL transformations to make the final HTML. Is this the way to go or are there other methods? Those built-in ASP.NET components (like DataGrid) are supposedly out of the question because both data and the desired presentation are too complex. -- |
X-G
Member #856
December 2000
|
Going the XSL route seems to be an unnecessarily roundabout way for no appreciable benefit. I don't know ASP, but if it's anything like how it's done in PHP, it's probably what I would do unless there were some special conditions that required some more complex representation. In short, I favor the KISS approach. Perhaps if you reveal more details we can help more? (Side note: Thanks to masking, I managed to cook together a neat map tool in just a day. Thanks, miran!) -- |
miran
Member #2,407
June 2002
|
Quote: Going the XSL route seems to be an unnecessarily roundabout way for no appreciable benefit. I don't know ASP, but if it's anything like how it's done in PHP, it's probably what I would do unless there were some special conditions that required some more complex representation. In short, I favor the KISS approach. Perhaps if you reveal more details we can help more? I don't know how it's done in PHP but in ASP you have plain HTML mixed with VB code which generates the dynamic parts of HTML. The existing application is written in ASP.NET which uses a different model (separate html from code) but the guy who wrote it decided to do it by generating the HTML tables on the fly in the code-behind code. So basically the code looks like this (pseudocode): string out = "<table>" for each record in recordset out = out + "<tr><td>" out = out + some data out = out + "</td></tr>" end for out = out + "</table>" write out to the page This of course looks extremely ugly and I'd rather not do it myself. Yes the XML way is less straightforward, but it seems to me a lot more organized. What I'm asking is whether it's worth the trouble or not. The thing is I have to make quite a good number of these tables. Quote: (Side note: Thanks to masking, I managed to cook together a neat map tool in just a day. Thanks, miran!) Woohoo. Hopefully one day I'll pick it up again and also finish the dialog designer tool. -- |
Tobias Dammers
Member #2,604
August 2002
|
In PHP, I'd do something like: <?php echo "<table>" while ($row = mysql_fetch_assoc($sqlresult)) { echo "<tr>"; foreach($row as $val) echo "<td>$val</td>"; echo "</tr>"; } echo "</table>"; ?>
Pretty much what you gave in pseudocode. --- |
CGamesPlay
Member #2,559
July 2002
|
For strictly tabular data, use a DataGrid. If it truly isn't capable, and you need to convince me of this, then use a Repeater control. -- Ryan Patterson - <http://cgamesplay.com/> |
ImLeftFooted
Member #3,935
October 2003
|
I like to code in my url strings, like so: The formating is done through CSS |
CGamesPlay
Member #2,559
July 2002
|
Ugh, that's not feasible in many situations, not related to this situation, and a flat-out Bad Idea. -- Ryan Patterson - <http://cgamesplay.com/> |
ImLeftFooted
Member #3,935
October 2003
|
Its obviously better then all of the alternatives. |
CGamesPlay
Member #2,559
July 2002
|
It has nothing to do with anything mentioned in this thread. He already has the data, he needs to render it to HTML. Exposing SQL injection doesn't make his presentation better. -- Ryan Patterson - <http://cgamesplay.com/> |
ImLeftFooted
Member #3,935
October 2003
|
But if you make a mistake in your SQL query its easier to fix! |
CGamesPlay
Member #2,559
July 2002
|
Yeah, because any site visitor can do it for you -- Ryan Patterson - <http://cgamesplay.com/> |
Ricardo Santos
Member #6,609
November 2005
|
That's a BAD BAD idea dustin! I'm a web designer myself and I've never seen such a thing, for obvious reasons! |
Rampage
Member #3,035
December 2002
|
Quote: One way I can think of is reading the data into XML and then using XSL transformations to make the final HTML The only case you would do that is when the data will be presented in many formats: HTML, PDF, XLS, CSV. If that's not the case, you don't really need to go that way; a direct query and a for loop will be enough. Quote:
I like to code in my url strings, like so: That's so a security risk! Yves would be delighted to test his 133t h4x0r skillz. -R |
Archon
Member #4,195
January 2004
|
Quote: What other options are there? I've done a little bit of both ASP and ASP.NET and you'll need to write the HTML yourself. In ASP.NET, there are webpage controls where it'll create a table/form/animation, or whatever, using functions (like an application). I don't know if that exists in ASP though. |
CGamesPlay
Member #2,559
July 2002
|
Quote: I've done a little bit of both ASP and ASP.NET and you'll need to write the HTML yourself. Quote: In ASP.NET, there are webpage controls where it'll create a table/form/animation, or whatever, using functions (like an application). I don't know if that exists in ASP though. <asp:repeater id="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <ItemTemplate> <div> <div><span>Date Completed</span> <%# ((WorkCode) Container.DataItem).completed.ToShortDateString() %> </div> <div><span>Work Code</span> <%# ((WorkCode) Container.DataItem).code %> </div> <div><asp:DataGrid id="equip" GridLines="Vertical" BorderStyle="None" AutoGenerateColumns="False" CellPadding="4" BorderWidth="1px" BorderColor="#DEDFDE" BackColor="White" runat="server" ForeColor="Black" Width="100%"> <Columns> <asp:TemplateColumn HeaderText="Equipment"> <ItemTemplate> <asp:Label runat="server" Text='<%# equipmentTA.FullNameById(((EquipRef) Container.DataItem).equipID) %>' ID="Label2"/> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="Time" HeaderStyle-Width="20%"> <ItemTemplate> <asp:Label runat="server" Text='<%# ((EquipRef) Container.DataItem).hours %>' ID="Label3"/> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid></div> </div> <hr /> </ItemTemplate> </asp:repeater>It's a little convoluted, but I tried to use a "practical" example (that's a simplified version of actual code from a .NET 1.1 application). The code behind this: protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) { WorkCode wt = (WorkCode) e.Item.DataItem; DataGrid equip = (DataGrid) e.Item.FindControl("equip"); equip.DataSource = wt.equip; equip.DataBind(); }You can see how I've nested a DataGrid into a Repeater to deliver complex information. The end product looks like this (I know it's terribly inefficient, I haven't gotten around to redoing it to look nicer): attachment -- Ryan Patterson - <http://cgamesplay.com/> |
miran
Member #2,407
June 2002
|
Wow. That's very close to exactly what I need! I'll give cookies later. -- |
CGamesPlay
Member #2,559
July 2002
|
I had a feeling it would be, it's a pretty common task -- Ryan Patterson - <http://cgamesplay.com/> |
BAF
Member #2,981
December 2002
|
I'd rather use a DB wrapper myself, like Pear::DB or MDB2. Also, DDustin, why in the world would you want to do that, and how does it make it any easier to fix your SQL? |
CGamesPlay
Member #2,559
July 2002
|
Quote: I'd rather use a DB wrapper myself, like Pear::DB or MDB2. Again, that has nothing to do with his problem. -- Ryan Patterson - <http://cgamesplay.com/> |
ImLeftFooted
Member #3,935
October 2003
|
CGamesPlay said: Yeah, because any site visitor can do it for you
Ricardo Santos said: That's a BAD BAD idea dustin! I'm a web designer myself and I've never seen such a thing, for obvious reasons! But why not let your visitors design your site? Thats the open source model isn't it |
miran
Member #2,407
June 2002
|
I just wanted to say thanks again. -- |
BAF
Member #2,981
December 2002
|
Quote: But why not let your visitors design your site? Thats the open source model isn't it {censored badness} I would like to report this to the EFW police! That is a blantant disregard for this weeks rules, three times in a row. |
bamccaig
Member #7,536
July 2006
|
I've done HTML creation in ASP and PHP and don't mind so much. Simple tables are relatively simple to generate. I don't know how complex your project is, but I'd say it's doable with simple generation code. Unfortunately, I have yet to do ASP.NET so I don't know how to use it to generate these things. Here's a simple example I just made up using ASP: Note: gobjProduct is an ADODB.Recordset containing Products... <% and %> are ASP opening and closing tags... ...etc. <table> <tr> <th>Product Name</th> <th>Product Price</th> </tr> <%Do Until gobjProduct.EOF%> <tr> <td> <%=Response.HTMLEncode(gobjProduct("Name"))%> </td> <td> <%=Response.HTMLEncode(FormatCurrency(gobjProduct("Price"), 2))%> </td> </tr> <%Loop%> </table> etc... Note that VBScript, the actual language used in this ASP example, is very Null sensitive. It pretty much requires you to check for Null before passing any value into a function. I can only hope that ASP.NET fixes this issue. I don't think it's an issue in PHP though. Dustin Dettmer said:
I like to code in my url strings, like so: Assuming you make these GET variables "safe" before embedding them in SQL it should still be okay. It's just odd to present the user with back-end terminology. It might be better if command was action (new | update | delete) and you use an id to keep track of which record. Make sure the logged in user has rights to do these things before allowing them to be done, etc... ORDER%20BY becomes order and fromTable should simply be from. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|