如何在MySQL数据库中保存HTML表 - 这样做的有效方法是什么? [关闭]

问题描述:

I have a php website with a html table. The user is able to modify each of the contained cells (much like in Excel), e.g. change it's content or font-size. My current approach is to save these cells in a one-to-many relationship: One mysql table for the different tables and one mysql table with the cells. To each "table-object" there are several "cell-objects" assigned. The problem I have at the moment is, that this generates really a lot of db querys (about 2200 when saving or opening one table, one for each cell). I think that this really is too much, isn't it (or at least not good way of practice)?

Can anyone give me a hint, what other way I could achieve this? Or is it ok like this anyway?

Thanks in advance,

katze_sonne

我有一个带有html表的php网站。 用户能够修改每个包含的单元格(非常类似于Excel),例如, 改变它的内容或字体大小。 我目前的方法是将这些单元格保存为一对多关系:一个用于不同表的mysql表和一个带有单元格的mysql表。 对于每个“表对象”,分配了几个“单元对象”。 我现在遇到的问题是,这会产生很多db查询(保存或打开一个表时大约2200,每个单元一个)。 我认为这真的太过分了,不是吗(或者至少不是很好的练习方法)? p>

任何人都可以给我一个提示,我能用其他方式实现这个目标吗? 或者它还是这样吗? p>

提前致谢, p>

katze_sonne p> div>

You could probably collect all the cell data into a multidimensional array of cells split into rows:

$table = array(
    array(0, 1, 2, 3, 4, 5),
    array(0, 1, 2, 3, 4, 5),
    array(0, 1, 2, 3, 4, 5)
);

Then serialize the whole array using PHP serialize() and save it in a single MySQL TEXT field.

When you read from the database, you can just unserialize() the field and it's back to being a normal PHP array which you can loop through and print out the table.

You can consider serializing your data to one of the following formats before sending it to the database to store

  1. Yaml
  2. JSon
  3. XML

All these are machine readable, plain text formats. So you can convert them back to objects in your code after retriving