Skip to content Skip to sidebar Skip to footer

Its Outputs Details Incorrectly In Php/html

I want to display all the questions from the drop down menu when the user selects All from the question drop down menu and output it underneath. Problem is that it is not doing thi

Solution 1:

Give this a try.......

$question = array();

while ($selectedstudentanswerstmt->fetch()) {
    // assuming you don't need the StudentId$questions[] = array('no' => $detailsQuestionNo,
                         'content' => $detailsQuestionContent);
}

and

foreach ($questionsas$key => $question) {
    echo'<p><strong>Question:</strong> ' . 
         htmlspecialchars($question['no']) .
         ': ' . 
         htmlspecialchars($question['content']) . 
         '</p>' . 
         PHP_EOL;
}

EDITED

Or you can try this if your grouping by question:

$question = array();

while ($selectedstudentanswerstmt->fetch()) {
    if (true === isset($questions[$detailsQuestionId])) {
        $questions[$detailsQuestionId]['students'][] = $detailsStudentId;
    } else {
        $questions[$detailsQuestionId] = array();
        $questions[$detailsQuestionId]['no'] = $arrQuestionNo;
        $questions[$detailsQuestionId]['content'] = $arrQuestionContent;
        $questions[$detailsQuestionId]['students'] = array();
        $questions[$detailsQuestionId]['students'][] = $detailsStudentId;
    }
}

foreach ($questionsas$questionId => $question) {
    // $question['no']// $question['content']foreach($question['students'] AS$key => $studentId) {
        // $studentId
    }
}

Or if your grouping by userID...

$students = array();

while ($selectedstudentanswerstmt->fetch()) {
    if (false === isset($students[$detailsStudentId])) {
        $students[$detailsStudentId] = array();
    }
    $students[$detailsStudentId][$detailsQuestionId] = 
                                       array('no' => $arrQuestionNo,
                                             'content' => $arrQuestionContent;
}

foreach ($studentsAS$studentId => $questions) {
    // $studentIdforeach ($questionsAS$questionId => $question) {
        // $questionId// $question['no']// $question['content']
    }
}

Solution 2:

Your foreach loop is iterating over the questions array, which was filled in with:

$questions[] = $arrQuestionNo;
        $questions[] = $arrQuestionContent;

This means it's an indexed array, not an associative array; the keys are 0 and 1.

But then you echo $arrQuestionNo[$key] and $arrQuestionContent[$key]. These are associative arrays, whose keys are student IDs, not indexes starting from 0 (unless you happen to have students with these ID numbers).

Also, you initialize $arrQuestionNo and $arrQuestionContent to an empty array each time through the fetch loop. So when you're echoing the results at the end, these just contain the questions from the last row that was fetched.

You should use a multidimensional array:

while ($selectedstudentanswerstmt->fetch()) {
  $questions[$detailsStudentId][$detailsQuestionNo] = $arrQuestionContent;
}

Then your printing loop should be:

foreach ($questionsas$studentId => $studentQuestions) {
  echo'<h2>Student '.htmlspecialchars($studentId).' Answers</h2>'. PHP_EOL;
  foreach ($studentQuestionas$questionNo => $content) {
    echo'<p><strong>Question:</strong> ' .htmlspecialchars($questionNo). ': ' .htmlspecialchars($content). '</p>' . PHP_EOL;
  }
}

Post a Comment for "Its Outputs Details Incorrectly In Php/html"