需要数组中的数组(我认为)

时间:2015-01-02 18:38:31

标签: php mysql arrays

我正在尝试使用PHP,jQuery,AJAX和数据库进行测验。但是我对第一个方面感到难过。

典型的测验将包含十个问题,每个问题后面跟着四个可能的答案 - 所有问题都在一页上。 HTML可能看起来非常像这样:

<div class="OuterWrapper">  
  <div class="question"><b>No one knows if there&#8217;s just one universe or a series of universes, commonly referred to as a:</b></div>
    <ul class="answers">
      <li class="Answer correct"><label><input data-key="A" type="radio">multiverse</label></li>
      <li class="Answer wrong"><label><input data-key="B" type="radio">parallel universe</label></li>
      <li class="Answer wrong"><label><input data-key="C" type="radio">constellation</label></li>
      <li class="Answer wrong"><label><input data-key="D" type="radio">black hole</label>     </li>
    </ul>
</div><!-- OuterWrapper -->

<div class="OuterWrapper">
  <div class="question"><b>Scientists believe the universe is:</b></div>
    <ul class="answers">
      <li class="Answer wrong"><label><input data-key="A" type="radio">shrinking</label></li>
      <li class="Answer correct"><label><input data-key="B" type="radio">expanding</label></li>
      <li class="Answer wrong"><label><input data-key="C" type="radio">in a state of equilibrium</label></li>
      <li class="Answer wrong"><label><input data-key="D" type="radio">becoming bipolar</label></li>
    </ul>
</div><!-- OuterWrapper -->

显示的PHP代码如下所示:

<div class="OuterWrapper">
  <div class="question"><b>'.$Question.'</b></div>
    <ul>
      <li class="Answer '.$Correct.'"><label><input data-key="'.$Value.'" type="radio">'.$Answer.'</label></li>
    </ul>
  </div>
</div>

我知道如何创建一个显示所有十个问题的数组。或者我可以创建一个显示所有答案的数组。但是,我不知道如何将两者结合起来,因此每个问题后面跟着与该问题相关的四个答案。

我假设我必须以某种方式将$ Answers数组放在$ Questions数组中?

echo join ($Questions, '').join ($Answers, '');

起初我放弃了,只是做了一个传统的查询,但这是一个噩梦 - 各种样式/语法问题。

无论如何,谁能告诉我如何创建我上面描述的数组?或者,如果有这种类型的数组的名称,那么只知道该名称将帮助我获取Google提示。

下面是一些更完整的代码,尽管它仍然大大缩小了。

$MyURL = 'gw-intro-1';

$stmt = $pdo->prepare("SELECT T.URL, TQ.QID, TQ.Question, TA.URL, TA.QID QID2, TA.Value, TA.Answer, TA.Correct
FROM g_tests T
LEFT JOIN g_test_questions TQ ON TQ.URL = T.URL
LEFT JOIN g_test_answers TA ON TA.URL = T.URL
WHERE T.URL = 'gw-intro-1' AND TQ.QID = TA.QID
ORDER BY TA.N
");
$stmt->execute(array(
 'MyURL'=>$MyURL
));

$Q1 = null;

while ($row = $stmt->fetch()) {
 $URL = $row['URL'];
 $QID = $row['QID'];
 $QID2 = $row['QID2'];
 $Question = $row['Question'];
 $Value = $row['Value'];
 $Answer = $row['Answer'];
 $Correct = $row['Correct'];
 $Correct = str_replace('1', 'correct', $Correct);
 $Correct = str_replace('2', 'wrong', $Correct);

 // Some aborted attempts at making arrays...
 $Questions[] = '<div class="question"><b>'.$Question.'</b></div>';
 $Answers[] = '<li class="Answer '.$Correct.'"><label><input data-key="'.$Value.'"    type="radio">'.$Answer.'</label></li>';
}

1 个答案:

答案 0 :(得分:0)

通过传递值数组

来执行预准备语句
$stmt = $pdo->prepare("SELECT T.URL, TQ.QID, TQ.Question, TA.URL, TA.QID QID2, TA.Value, TA.Answer, TA.Correct
FROM g_tests T
LEFT JOIN g_test_questions TQ ON TQ.URL = T.URL
LEFT JOIN g_test_answers TA ON TA.URL = T.URL
WHERE T.URL = :MyURL AND TQ.QID = TA.QID
ORDER BY TA.N
");

$stmt->execute(array(
 ':MyURL' => $MyURL
));

如果您期望超过1行,请使用

PDOStatement::fetchAll