Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示测验的结果_Php_Mysql - Fatal编程技术网

Php 显示测验的结果

Php 显示测验的结果,php,mysql,Php,Mysql,我必须为我的公司员工做一个测验,我在显示测验结果时有一个逻辑问题 数据库结构: +-----------+ | question | +-----------+ | id PK | | text | +-----------+ +-----------------+ | answer | +-----------------+ | id PK | | question_id FK | | text | | cor

我必须为我的公司员工做一个测验,我在显示测验结果时有一个逻辑问题

数据库结构:

+-----------+
| question  |
+-----------+
| id     PK |
| text      |
+-----------+

+-----------------+
| answer          |
+-----------------+
| id           PK |
| question_id  FK |
| text            |
| correct         |
+-----------------+

+--------------+
| user_result  |
+--------------+
| user_id   PK |
| answer_id PK |
+--------------+
SELECT 
    q.id,
    COUNT(a.id) as total_answers,
    SUM(a.correct) as correct_answers
FROM
    question q
INNER JOIN
    answer a ON (q.id = a.question_id)
INNER JOIN
    user_result ur ON (ur.answer_id = a.id)
GROUP BY
    q.id
  • 问题-我从接口插入的地方:id,questiontext

  • 答案-我有:id、问题id、answertext和correct,其值可以为1或0,如果答案正确,则为1,如果答案不正确,则为0。我的问题可以有多个正确答案(这就是为什么我被卡住了)

  • User_result-其中我有id、question_id、answer_id、correct,可以是1或0,如答案表中所示,User_test告诉我他参加了哪项测试,基于他有多少问题的级别和唯一的用户id

问题:

+-----------+
| question  |
+-----------+
| id     PK |
| text      |
+-----------+

+-----------------+
| answer          |
+-----------------+
| id           PK |
| question_id  FK |
| text            |
| correct         |
+-----------------+

+--------------+
| user_result  |
+--------------+
| user_id   PK |
| answer_id PK |
+--------------+
SELECT 
    q.id,
    COUNT(a.id) as total_answers,
    SUM(a.correct) as correct_answers
FROM
    question q
INNER JOIN
    answer a ON (q.id = a.question_id)
INNER JOIN
    user_result ur ON (ur.answer_id = a.id)
GROUP BY
    q.id
我的问题是,如果一个问题有多个选项,如果一个问题只有一个正确答案,我无法根据用户给出的答案显示用户结果,这很简单:

$eval = round(($score/40)*100,2);  
其中,$score是所有答案中包含correct='1'的
mysql\u num\u行。40是他必须回答的问题的数量

我也有40、60、80和100个问题的不同类型的测验-我在测验管理中心分别有它们,我还知道哪个用户参加了哪个测试,因为我在数据库中有记录

显示结果的最佳方式是什么

$restest = mysql_query("SELECT * 
                          FROM Tests 
                         WHERE mediu3luni='mediu3luni'", $testaredb);
while($test = mysql_fetch_assoc($restest))
{
    $resvar = mysql_query("SELECT * 
                             FROM Variante 
                            WHERE id_intrebare='{$test['id_intrebare']}' AND 
                                  corect='1'", $testaredb);
    $toatecorecte = mysql_num_rows($resvar);
    $toatevar = mysql_fetch_assoc($resvar);
    echo 'Question'.$toatevar['id_intrebare'].' has'.$toatecorecte.' correct answers<br>';
}

上面的代码也只选择测试中的问题,这是从管理面板配置的。

您的文章在以下几个方面令人困惑:1)数据库表的名称与示例代码不一致。2) 您要求用户提供正确答案的数量,但您的示例是正确回答问题的次数

编辑:修订时考虑了对该答案的评论

因此,使用“数据库结构”名称:

用户正确回答的数量:

SELECT count(question_id) AS score
FROM   User_result
WHERE id = 'user_id' and user_test = 'a' and correct = 1

您可以执行类似的操作,以在测验中返回正确答案的数量

<?php
$id = 12;

$query = "... WHERE q.id = $id";

$result = mysqli_query($query);
while ($row = mysqli_fetch_assoc($result)) {
    $total = $row['total_answers'];
    $correct = $row['correct_answers'];
    $percentage = round((($correct / $total) * 100), 2);

    echo 'Correct answers given by all users for question ' . $row['id'] . ': ' . $correct . ' (' . $precentage . '%)<br />';
}
ERM:

+-----------+
| question  |
+-----------+
| id     PK |
| text      |
+-----------+

+-----------------+
| answer          |
+-----------------+
| id           PK |
| question_id  FK |
| text            |
| correct         |
+-----------------+

+--------------+
| user_result  |
+--------------+
| user_id   PK |
| answer_id PK |
+--------------+
SELECT 
    q.id,
    COUNT(a.id) as total_answers,
    SUM(a.correct) as correct_answers
FROM
    question q
INNER JOIN
    answer a ON (q.id = a.question_id)
INNER JOIN
    user_result ur ON (ur.answer_id = a.id)
GROUP BY
    q.id
您只需知道
答案
用户
给出了什么,与
问题
的关系已经在
答案

查询:

+-----------+
| question  |
+-----------+
| id     PK |
| text      |
+-----------+

+-----------------+
| answer          |
+-----------------+
| id           PK |
| question_id  FK |
| text            |
| correct         |
+-----------------+

+--------------+
| user_result  |
+--------------+
| user_id   PK |
| answer_id PK |
+--------------+
SELECT 
    q.id,
    COUNT(a.id) as total_answers,
    SUM(a.correct) as correct_answers
FROM
    question q
INNER JOIN
    answer a ON (q.id = a.question_id)
INNER JOIN
    user_result ur ON (ur.answer_id = a.id)
GROUP BY
    q.id
我们在这里主要做的是获取每个
问题
的每个
答案
的所有
用户结果
s,按
问题
s ID分组。然后我们计算
用户结果
s(给出所有答案)并汇总每个
正确
标志,这导致了这样一个等式:
0+1+1+0+0…
,这是所有用户给出的正确答案的数量

如果需要特定用户或特定问题,只需将此条件添加到查询的
WHERE
部分即可。这适用于多个正确答案,也适用于根据需要在多个答案上使用单个答案

PHP:

+-----------+
| question  |
+-----------+
| id     PK |
| text      |
+-----------+

+-----------------+
| answer          |
+-----------------+
| id           PK |
| question_id  FK |
| text            |
| correct         |
+-----------------+

+--------------+
| user_result  |
+--------------+
| user_id   PK |
| answer_id PK |
+--------------+
SELECT 
    q.id,
    COUNT(a.id) as total_answers,
    SUM(a.correct) as correct_answers
FROM
    question q
INNER JOIN
    answer a ON (q.id = a.question_id)
INNER JOIN
    user_result ur ON (ur.answer_id = a.id)
GROUP BY
    q.id
在PHP中,您可以执行以下操作:

<?php
$id = 12;

$query = "... WHERE q.id = $id";

$result = mysqli_query($query);
while ($row = mysqli_fetch_assoc($result)) {
    $total = $row['total_answers'];
    $correct = $row['correct_answers'];
    $percentage = round((($correct / $total) * 100), 2);

    echo 'Correct answers given by all users for question ' . $row['id'] . ': ' . $correct . ' (' . $precentage . '%)<br />';
}

能否提供一些示例数据和所需结果(如果有完整的SQL查询,还可以提供完整的SQL查询)?一、 首先,不明白问题是什么。我发布了一个编辑,这是我从现在开始所做的,通过汇总所有可能的问题答案,我将找出用户必须有多少正确答案才能获得100%的分数。但是我不知道怎么把它们加起来。在此之后,公式如下:$eval=round($score/$totalchoices)*100,2);我理解正确吗?您想显示所有用户给出的正确答案的百分比吗?是的,但我不知道当一个问题有多个答案时如何显示,如果只有一个答案,则将是简单的用户答案=问题,但如果一个问题有多个正确答案,正确答案的百分比=用户正确答案/总正确答案*100,2。我不知道如何从数据库中找出全部正确答案。我可能有点困惑,所以我再次解释:我有问题a,其中有2个正确答案,问题B,其中只有1个正确答案。如果测试只有这2个问题,用户必须有3个正确答案,2个来自a问题,1个来自B问题。为了计算他的结果,我必须知道所有正确答案和用户正确答案的数量。有了这两件事,我可以计算他的分数。我不知道如何从数据库中获得一组特定问题的所有正确答案的数量。我会试试你贴的。谢谢不,我贴的不是你想要的。我会修改它。非常感谢。我会尝试看看它对我的情况有什么作用。不幸的是,它对我没有帮助,但非常感谢你的尝试。你遇到了什么问题?它没有返回任何东西,但我设法做到了。帮了我很多你的想法与内在的连接。谢谢,不客气。也许查询与您的确切数据库设计不匹配,这就是为什么我建议使用一个更简单的查询,并说您可以尝试类似的方法;)