简体   繁体   English

在数据库中存储HTML表的数据

[英]Storing data for HTML tables in database

I'm developing an application using PHP / SQL Server that needs to store information on graduates from tons of different academic programs, along with information on how many were employed after graduating... This will then be queried by users, who will want to compare one program to another, so they will need to be able to generate custom tables showing the outcomes for students. 我正在使用PHP / SQL Server开发一个应用程序,该应用程序需要存储有关成千上万种不同学术课程的毕业生的信息,以及有关毕业后受雇的人数的信息...然后,用户会查询该信息。将一个程序与另一个程序进行比较,因此他们将需要能够生成显示学生成绩的自定义表格。 Tables will be something like this: 表将是这样的:

Program            Salary after 1 Year      Salary after 2 Years         Etc...
Engineering        Lots                     Even more!
Philosophy         Nothing                  Are you kidding me?

So, here's the question: should I store individual values for these data points in the database, or should I just store the raw HTML for the table row? 因此,这是一个问题:我应该在数据库中存储这些数据点的单个值,还是只存储表行的原始HTML?

In other words, do I store this: 换句话说,我是否存储:

ProgramID    Salary1    Salary2               Etc...
1            Lots       Even More!
2            Nothing    Are you kidding me?

Or do I store this: 还是我存储这个:

ProgramID   RowHTML
1           <td>Lots</td><td>Even More!</td>...
2           <td>Nothing</td><td>Are you kidding me?</td>

I can see arguments for both approaches... my gut tells me that storing the raw HTML will be faster - it will eliminate GOBS of PHP processing. 我可以看到两种方法的参数...我的直觉告诉我,存储原始HTML会更快-它将消除PHP处理的GOBS。 But I'm open to other interpretations. 但是我愿意接受其他解释。

Simple Rule for any RDBMS , only store the data in your tables and nothing else. 任何RDBMS的简单规则,仅将数据存储在表中,而不存储其他数据。

This will make your data retrieval, updated and insert operations fast, simple and safe . 这将使您的数据检索,更新和插入操作变得快速,简单和安全。 Store data in your tables and create HTML tables on runtime. 将数据存储在表中,并在运行时创建HTML表。 I have demonstrated here how easy and simple it is to create HTML tables when you have only data stored in your table and nothing else. 我在这里演示了仅在表中存储数据而没有其他内容时创建HTML表是多么容易和简单。

Example

I have used your given example 我用你给的例子

DECLARE @t TABLE (ProgramID INT,Salary1 VARCHAR(50),Salary2 VARCHAR(50))
INSERT INTO @t VALUES
(1,'Lots','Even More!'),
(2,'Nothing','Are you kidding me?')

DECLARE @tableHTML  NVARCHAR(MAX) ;

SET @tableHTML =
    N'<H1>Students Salaries</H1>' +
    N'<table border="1">' +
    N'<tr><th>ProgramID </th><th>Salary1</th><th>Salary2</th>' +
    CAST ( ( SELECT td = ProgramID,       
                    '',
                    td = Salary1, 
                    '',
                    td = Salary2
                FROM @t    

              FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX) ) +
    N'</table>' ;
SELECT @tableHTML;

HTML Table Created by the above query 由上述查询创建的HTML表

<H1>Students Salaries</H1><table border="1"><tr><th>ProgramID </th><th>Salary1</th><th>Salary2</th><tr><td>1</td><td>Lots</td><td>Even More!</td></tr><tr><td>2</td><td>Nothing</td><td>Are you kidding me?</td></tr></table>

I would store the raw data only. 我只会存储原始数据。 this will provide the ability to change the format/layout of the page when your boss says "Make it look newer" or whatever. 当老板说“让它看起来更新”或其他内容时,这将提供更改页面格式/布局的功能。 If you store the table data, you are stuck. 如果存储表数据,则会卡住。

我认为在数据库中存储这些数据点的单个值将更好地管理应用程序,HTML很好,但是当您使用PHP-DATAbase时更好,...祝您好运。

So, here's the question: should I store individual values for these data points in the database, or should I just store the raw HTML for the table row? 因此,这是一个问题:我应该在数据库中存储这些数据点的单个值,还是只存储表行的原始HTML?

Straight answer: 直接回答:

Store the data points , the html you can always recreate later, this will also make your queries way more fast. 存储data points ,您以后可以随时重新创建html,这也将使查询更快。

Short answer: Store the individual data, not the HTML. 简短答案:存储单个数据,而不是HTML。

There's no benefit to storing HTML here. 在这里存储HTML没有任何好处。 Reproducing the table might be a little easier, but you're also storing a lot of redundant information, and building tables dynamically is not a difficult task to begin with. 再现表可能会更容易一些,但是您还存储了很多冗余信息,并且动态地开始构建表并不是一件容易的事。 It's also much easier to manipulate raw data: when you're storing salaries over time as a single row of HTML, then you can only sort by program, but if you store each year individually and control the data type (your example salaries are strings, but you should be storing integers or floats, since you know that salary is an amount of money), you have the option of sorting programs by how much money graduates make out of college, ten years down the road, etc. Also bear in mind that you might not always want to display a table: you might want to graph the income over time for each major, for example, in which case you'd need to extract the salaries from the and tags, creating more work for yourself and slowing down the application. 处理原始数据也要容易得多:当您将薪水作为HTML单行存储时,则只能按程序排序,但是如果您分别存储每年并控制数据类型(您的薪水示例为字符串) ,但您应该存储整数或浮点数,因为您知道薪水是一笔钱),因此您可以选择按毕业生从大学赚取的收入,未来十年的收入等进行分类的程序。请注意,您可能并不总是希望显示表格:例如,您可能希望绘制每个专业的一段时间收入图,在这种情况下,您需要从和标记中提取薪水,从而为自己和减慢应用程序的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM