php - 组合数组和 MySQL 结果

标签 php mysql

我有一个使用数组存储问题的测验:

$array=array(
        '1'=> array('1','What does everyone know when they see it?','quest1','Good     publicity','Bad punctuation', 'Good business writing', 'Bad spelling','../images/example03.jpg'),
        '2'=> array('2','What do people write instead of \'now\'?','quest2','Presently','At this moment in time', 'Currently', 'At the present time','../images/example03.jpg'),
);

然后我使用这个循环显示问题:

foreach($array as $quiz)
{
echo            
                '<div class="question">
                        <div class="questionList">
                            <h4>' . $quiz[0] . '. <strong>' . $quiz[1] .         '</strong></h4>
                                <ul>
                                    <li><input type="radio" name="' . $quiz[2] . '" value="1"> '. $quiz[3] . '</li>
                                    <li><input type="radio" name="' . $quiz[2] . '" value="2"> '. $quiz[4] . '</li>
                                    <li><input type="radio" name="' . $quiz[2] . '" value="3"> '. $quiz[5] . '</li>
                                    <li><input type="radio" name="' . $quiz[2] . '" value="4"> '. $quiz[6] . '</li>
                                </ul>
                        </div>
                    <img src="'. $quiz[7] . '" class="questionImg01" />
                    <div class="clr"></div>
                </div>
                ';
    }

}

如果有人第一次没有回答所有问题,我会在 MySQL 中存储每个问题的值,如果没有回答则存储“0”。我想检查用户已经回答的单选按钮,但不知道如何组合测验数组和 MySQL 结果。

结果的表结构是:

测验编号 |用户名 |任务1 |任务2 |任务3 |任务4 |任务5 |任务6 |任务7 |任务8 |任务9 |任务10 |完成

其中 quiz_id 是该测验的引用编号,complete 是一个标志,用于记录用户是否已回答所有问题。我正在检索这样的结果:

$results = mysql_query("SELECT * FROM quiz_results_baseline WHERE username = ('$username')");

有什么方法可以将这两者结合起来吗?

我还有一个问题,我无法让这个查询包含 WHERE 语句 - 我想将所有测验结果存储在一个表中,然后使用 quiz_id 引用每个用户的测验结果:

mysql_query("INSERT INTO quiz_results_baseline (quest1,quest2,quest3,quest4,quest5,quest6,quest7,quest8,quest9,quest10,username,quiz_id,complete) VALUES $queryData ON DUPLICATE KEY UPDATE quest1=VALUES(quest1),quest2=VALUES(quest2),quest3=VALUES(quest3),quest4=VALUES(quest4),quest5=VALUES(quest5),quest6=VALUES(quest6),quest7=VALUES(quest7),quest8=VALUES(quest8),quest9=VALUES(quest9),quest10=VALUES(quest10),complete=VALUES(complete)");

如有任何帮助,我们将不胜感激!

最佳答案

为了使完​​成一半的测验结果与测验一起呈现,请尝试以下代码。

$questions=array(
  '1'=> array('1','What does everyone know when they see it?','quest1','Good     publicity','Bad punctuation', 'Good business writing', 'Bad spelling','../images/example03.jpg'),
  '2'=> array('2','What do people write instead of \'now\'?','quest2','Presently','At this moment in time', 'Currently', 'At the present time','../images/example03.jpg'),
);

// do your query for the row of answers
// $answers = mysql_fetch_assoc(...);

// for testing
$answers = array(
 'quest1' => '1',
 'quest2' => '4');


for($i = 1; $i <= count($questions); $i++)
{
  $quiz = $questions[$i];
  $answer = $answers[$quiz[2]];

  echo            
    '<div class="question">
        <div class="questionList">
          <h4>' . $quiz[0] . '. <strong>' . $quiz[1] . '</strong></h4>
          <ul>
            <li><input type="radio" name="' . $quiz[2] . '" value="1" ' . (($answer == '1') ? 'checked' : '') . '> '. $quiz[3] . '</li>
            <li><input type="radio" name="' . $quiz[2] . '" value="2" ' . (($answer == '2') ? 'checked' : '') . '> '. $quiz[4] . '</li>
            <li><input type="radio" name="' . $quiz[2] . '" value="3" ' . (($answer == '3') ? 'checked' : '') . '> '. $quiz[5] . '</li>
            <li><input type="radio" name="' . $quiz[2] . '" value="4" ' . (($answer == '4') ? 'checked' : '') . '> '. $quiz[6] . '</li>
          </ul>
        </div>
      <img src="'. $quiz[7] . '" class="questionImg01" />
      <div class="clr"></div>
    </div>
    ';
}

您必须执行查询才能获得 $answers 数组,但我提供了一个示例,以便您了解如何使用结果。

我运行了代码,结果如下所示:

results

希望对您有所帮助!

关于php - 组合数组和 MySQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10769072/

相关文章:

PHP 再次为 sql 查询设置变量值不起作用

mysql - 添加 header 以跨连接查询

php - 在数据库中查找可能的重复项

PHP 写入二进制响应

PHP:如何重命名使用 Zend_Form_Element_File 上传的文件?

在 codeIgniter 的 View 部分中编写的 Javascript 不起作用

php - "I' m "becomes "I\'m"在 PHP 中? (为什么要添加斜杠?)

mysql - 如何定义top-3评级?

mysql - 系统变量 "MESSAGE_TEXT"不适用于 Mysql 5.6 中的 SIGNAL 语句?

php - mysql 和 php 中无向、未加权图中 2 个节点之间的所有最短路径