0

I want to create some online quizzes using values stored in a database table. Each page will display ten questions, with each question followed by four possible answers...

What color are apples?
red
yellow
blue
pink

I know how to make a simple array that will display all the questions. Or I can display all the answers. But grouping each set of answers with the appropriate answer is trickier.

Someone suggested I use a multidimensional array combined with foreach. So I started checking out some related threads, including the one @ php PDO fetchAll() - while not working, foreach works but I'm completely snowed.

This is what I've come up with so far...

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

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

But it probably has more errors than I can count. Is this at least on the right track?

I posted my code below.

$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 />';
    }
}

I edited this to show my revised code:

$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";
 }
}
Community
  • 1
  • 1
  • I just discovered that this code - echo $Questions ['question']; - echoes each several questions multiple times when inserted inside the loop. When echoed outside the loop, it just echoes one question. I can't yet display any answers. –  Jan 02 '15 at 19:48

1 Answers1

0

You could, alternatively, try $stmt->fetchAll(PDO::FETCH_ASSOC). That does directly give you an array of datasets. The structure will be as follows :

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

You may loop through this kind of array using

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

Thus you could leave out

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 />';
    }
 }
Tacticus
  • 561
  • 11
  • 24
  • Thanks, but I'm confused. Am I supposed to change my query to $stmt->fetchAll(PDO::FETCH_ASSOC)("SELECT or $stmt->fetchAll("SELECT Either one gives me error messages. –  Jan 02 '15 at 20:02
  • After you executed the statement, you may fetch it using ´$stmt->fetchAll(PDO::FETCH_ASSOC)´. That should give you an array with the described structure, that you can loop through with the given function. – Tacticus Jan 02 '15 at 20:14
  • I see. I made the changes, but it still isn't working. I edited my original post to show my new code. I just realized I did something really stupid, though. Instead of putting my questions and answers in separate database tables, I should have put them all in one table, in a parent-child relationship. –  Jan 02 '15 at 20:25
  • See my edited version of your question. The `$stmt->fetchAll();` function already returns an array, that the loops below walk through. If you have any problems, please let me know. – Tacticus Jan 02 '15 at 20:32
  • I made the changes. However, I now get two errors - Undefined variable: Array and Warning: Invalid argument supplied for foreach() –  Jan 02 '15 at 20:44
  • Try replacing `$stmt->execute(array('MyURL' => $MyURL));` with `$stmt->execute();`. However, I also suggest using a very easy SQL statement, such as `SELECT * FROM g_tests` , if `g_tests` is the correct table. – Tacticus Jan 02 '15 at 20:53