多维数组+ Foreach

时间:2015-01-02 19:38:42

标签: php mysql arrays multidimensional-array

我想使用存储在数据库表中的值创建一些在线测验。每个页面将显示十个问题,每个问题后跟四个可能的答案......

What color are apples?
red
yellow
blue
pink

我知道如何制作一个能够显示所有问题的简单数组。或者我可以显示所有答案。但是用适当的答案对每组答案进行分组是比较棘手的。

有人建议我使用结合foreach的多维数组。所以我开始查看一些相关的主题,包括@ php PDO fetchAll() - while not working, foreach works,但我已经完全下雪了。

这是我到目前为止所提出的......

$Questions = array('question' => $Question, 'answers' => array($Answers));

foreach ($Questions as $Question => $Answers) {
  echo $Questions.'<br />';
 }

但它可能有比我更重要的错误。这至少是在正确的轨道上吗?

我在下面发布了我的代码。

$stmt = $pdo->prepare("SELECT T.URL, TQ.URL, TQ.QID, TQ.Question, TQ.Feedback, 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
));

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

    $Questions = array('question' => $Question, 'answers' => array($Answers));

    foreach ($Questions as $Question => $Answers) {
        echo $Questions.'<br />';
    }
}

我编辑了这个以显示我修改后的代码:

$stmt = $pdo->prepare("SELECT T.Site, T.Type, T.URL, T.Section, T.URL_Foreign, T.Title, T.Subtitle, T.Parent, T.Live, TQ.URL, TQ.QID, TQ.Question, TQ.Feedback, 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));

$Array = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($Array as $dataset){
 foreach ($dataset as $Column => $Value){
    echo "The column [$Column] contains [$Value] <br> \r\n";
 }
}

1 个答案:

答案 0 :(得分:1)

您也可以尝试$stmt->fetchAll(PDO::FETCH_ASSOC)。这确实直接为您提供了一系列数据集。结构如下:

Array ( "datasetNumber" =>
    Array (
        "Row1Name" => "Row1Value",
        "Row2Name" => "Row2Value",
        ...                 
    )
)

您可以使用

循环遍历此类数组
foreach ($Array as $dataset){
    foreach ($dataset as $Column => $Value){
        echo "The column [$Column] contains [$Value] \r\n";
    }
}

因此你可以省略

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

    $Questions = array('question' => $Question, 'answers' => array($Answers));

    foreach ($Questions as $Question => $Answers) {
         echo $Questions.'<br />';
    }
 }