如何存储和显示HTML m:n数组(二维表)

时间:2016-10-23 07:11:54

标签: html data-structures orm relationship denormalization

如何组织信息(在MVC模型中)以表示这个真实的例子:

调查 - 可以有多个问题和多个会回答问题的人

问题 - 只有单一答案 - 是或否

PersonAnswer - 回答问题的人

示例显示:

N to M informational array

到目前为止我的实施是:

DB

调查 - 问题 - 一对多

问题 - PersonAnswer - 一对多

控制器代码: - 伪代码

  1. Survey.FindAllQuestions
  2. 每个问题循环并找到所有答案
  3. 结果是地图 - Survey = [Question1:AnswerList1,Question2:AnswerList2]
  4. 在地图上的HTML循环中,我能够显示信息
  5. 我的方法存在问题:

    1. 标题 - 应计算人名
    2. 需要排序列表,否则答案将会混合
    3. 如果该人没有回答问题,如何正确管理 - 我应该将其存储在数据库中吗?
    4. 更新:我使用的是java和mysql。

1 个答案:

答案 0 :(得分:1)

我对此问题的处理方法首先是处理数据库:

在我的情况下,我就是这样做的:

Have a table of 

  users->represents db users
  surveyitems->with item(questions) and id(probaby autoincrement integer pkey),

 surveycheks->representing the users and their checks having({
    id(pkey),
    checkid(foreign_key ->to surveyitems table id)
    yeschecked(representing the yes)
    nochecked(representi)ng the nos
  })

然后在你的视图中,例如当使用yii php framework

use gii tool to generate models and crud //here you can use your own
  logic of saving data to db

  On your survey form
   1. load all surveys from table surveyitems with id and items()// display  item
   2. Have radio buttons with values of id(from survey items);

   3.. when saving loop through all checked yes or nos and store them in surveycheks

我希望你明白这个想法