代码之家  ›  专栏  ›  技术社区  ›  AlexanderJohannesen

用于存储动态二维数组的SQL设计和/或PHP类?

  •  1
  • AlexanderJohannesen  · 技术社区  · 17 年前

    我需要一种优雅的方式来存储各种大小(x和y)的动态数组(基本上是没有所有功能的电子表格),主要用作枚举、列表、查找数据、价格表之类的东西。多语言将是一个巨大的好处。精髓的速度。

                |  1 |  2 |   3 |   4
      -------------------------------
      model A   | 2$ | 5$ |  8$ | 10$
      model B   | 3$ | 6$ |  9$ | 12$
      model C   | 4$ | 8$ | 10$ | 13$
    

    所以,为了获得信息,我会这样做;

      $price = this_thing_im_after ( '3', 'model B' ) ;
      echo $price ;   // Prints '9$'
    

    我在PHP5和Zend框架的世界里,但是关于设计和SQL的想法也很不错,甚至是来自外部世界、LIB、扩展等方面的建议,因为我不想重新发明太多的轮子。我最需要后端的东西,稍后我将为动态工作表编写GUI。想法、想法、指针?

    只是一个编辑,指出我不想序列化和blob数据,因为我想查询索引和表,甚至可能是数据(或支持这种类型的人的类型,现在) 那个

    2 回复  |  直到 13 年前
        1
  •  2
  •   jmucchiello    17 年前

      CREATE TABLE sheet (
          sheet_id int not null,
          name varchar(32),
          rows int, -- stores max dimension if needed
          cols int, -- stores max dimension if needed
          primary key (sheet_id)
      );
      CREATE TABLE cells (
          cell_id identity, -- auto inc field for ease of updates
          sheet_id int not null, -- foreign key to sheet table
          row int not null,
          col int not null,
          value smalltext, -- or a big varchar depending on need
          primary key (cell_id), -- for updates
          unique index (sheet_id, row, col), -- for lookup
          index (value) -- for search
      );
    
      CREATE TABLE row_labels (
          sheet_id int not null,
          row int not null,
          label varchar(32),
          primary key (sheet_id, row)
      );
    
      CREATE TABLE col_labels (
          sheet_id int not null,
          col int not null,
          label varchar(32),
          primary key (sheet_id, col)
      );
    

    这样可以很好地分割数据:

     // Slice [4:20][3:5]
     SELECT row, col, value FROM cells
     WHERE sheet_id = :sheet
       AND row BETWEEN 4 AND 20
       AND col BETWEEN 3 AND 5
     ORDER BY row, col
    
     while ($A = fetch()) {
         $cell[$A['row'][$A['col']] = $A['value']; // or unserialize($A['value']);
     }
    
        2
  •  0
  •   Tomalak    17 年前

    有必要只取一个吗 电子表格,还是按包含的数据进行查询?

    如果您只想存储和检索整个内容,我将只使用数组的明确文本表示(例如。 serialize() )并将其存储为文本。

    推荐文章