使用PHP和SQL对问卷数据进行排序 [英] Sorting questionnaire data using PHP and SQL

查看:49
本文介绍了使用PHP和SQL对问卷数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 HTML、PHP 和 SQL 创建了一个简单的多项选择问卷,提交后,会将数值添加到表格中,每个数值代表每个问题的特定答案.

I have created a simple multiple choice questionnaire using HTML, PHP and SQL, which when submitted, adds numeric values to a table which each represent specific answers to each of the questions.

例如,我有四个问题,一个提交的表单可能会在我的数据库单元格中显示为0,1,3,1"(因此,问题 1 的第一个选项为0",问题的答案为1"2 等;).

For example, I have four questions, and one submitted form may appear in my database cell as '0,1,3,1' (So answered the first option '0' for Question 1, and '1' for Question 2 etc;).

我现在正在尝试输出该数据,并计算有多少人选择了每个问题的答案.

I'm now trying to output that data, and count how many people have selected which answer on each question.

如何遍历表中的数据并让 PHP 每次递增某种数组,以便我最终计算出这么多人对问题 X 回答 X"的计数.

How can I loop over that data in the table and get PHP to increment some kind of array each time so that I eventually have a count of 'This many people answered X for Question X'.

我有点困惑!提前致谢.

I'm a little confused! Thanks in advanced.

/* 编辑 */

我的数据库布局很简单(可能是我哪里出错了),很简单:

My database layout is simple (possibly where I'm going wrong), it's simply:

问卷 ID |结果

1 --------------------0,1,3,2

1 --------------------0,1,3,2

(无法很好地布置表格,但希望这很清楚)

(Couldn't get the table to layout well but hopefully that's clear)

至于我到目前为止所尝试的,不是我想不出该怎么做.

As for what I have tried so far, not much as I couldn't think what to do.

我有两个 for 循环,一个嵌套在另一个循环中,相对于我的问题数量执行,为每个问题和答案创建空数组.

I've got two for loops, one nested inside the other, that execute relative to the amount of questions I have, which create empty arrays for each question and answer.

现在我需要用每个答案被选择的次数的计数填充这些数组.

Now I need to fill these arrays with counts of how many times each answer has been selected.

推荐答案

为什么不在原来的 SQL 中做,即.

Why not do it in the original SQL, ie.

"select question, answer, count(answer)
group by question, answer"

这应该会输出您的问题列表、选择的答案以及选择的次数...

This should then output a list of your questions, the answers that were chose, and how many times...

这篇关于使用PHP和SQL对问卷数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆