Link to home
Create AccountLog in
Avatar of Bruno1990

asked on

How to get the table below to be displayed correctly

Want to get the data in the html table displayed correctly
I have a question on how to display the correct table layout in my php code:

I want to display answers and their text inputs in a table.. Now at the moment it is displayed as below:

QuestionNo   Question                          Answer        Marks Per Answer    
1                     What is 2+2                        B                   (text input)        
2                     Name the 3 hobbits?      BCE              (text input)  

I want to change the display of the table so that it looks like this below:

 QuestionNo  Question                       Answer        Marks Per Answer
 1                   What is 2+2?                     B                    (text input)
 2                   Name the 3 Hobbits?     B                   (text input)                  
                                                                     C                    (text input)
                                                                     E                    (text input)

As you can see from the new display. I want the each answer per question to be displayed in thier own row, not all answers per question in in one row which is what it is doing at moment.

I want the text inputs to also be display in its own row, like the answers:
My question is that how can point 1 and 2 be achieved so that it can match the new layout?

Below is the code for the current display:

$assessment = $_SESSION['id'] . $sessionConcat;


$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, 
FROM Session s 
INNER JOIN Question q ON s.SessionId = q.SessionId
JOIN Answer an ON q.QuestionId = an.QuestionId AND an.SessionId = q.SessionId
WHERE s.SessionName = ?
GROUP BY an.SessionId, an.QuestionId
ORDER BY q.QuestionId, an.Answer

// prepare query
// You only need to call bind_param once
$stmt->bind_param("s", $assessment);
// execute query

// This will hold the search results
$searchQuestionId = array();
$searchQuestionContent = array();
$searchAnswer = array();

// Fetch the results into an array

// get result and assign variables (prefix with db)
$stmt->bind_result($dbQuestionId, $dbQuestionContent, $dbAnswer);
while ($stmt->fetch()) {
$searchQuestionId[] = $dbQuestionId;
$searchQuestionContent[] = $dbQuestionContent;
$searchAnswer[] = $dbAnswer;




<form id="QandA" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">


echo "<table border='1' id='markstbl'>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>

foreach ($searchQuestionContent as $key=>$question) {
echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td class="answertd">'.htmlspecialchars($searchAnswer).'</td>' . PHP_EOL; 
echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
echo "</table>" . PHP_EOL;




Open in new window

Below is what the Session, Question and Answer Table looks like:

Session Table:

SessionId (auto)  SessionName
1                               AAA
2                               AAB
Question Table:

SessionId  QuestionId (auto)  QuestionContent
1                  1                                    What is 2+2?
1                  2                                    Name the 3 hobbits?

Answer Table:

AnswerId (auto) SessionId  QuestionId   Answer
1                                1                   1                      B  
2                                1                   2                      B
3                                1                   2                      C
4                                1                   2                      E
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

At line 38, please put these statements and show us what the data looks like.

Avatar of Bruno1990


When I have done the var_dumps above as you have stated, it outputs this:

array(2) { [0]=> int(1) [1]=> int(2) } array(2) { [0]=> string(18) "Here are 2 answers" [1]=> string(23) "Here is a single answer" } array(2) { [0]=> string(1) "B" [1]=> string(4) "True" }

This is what the table content looks like:

QuestionNo.    Question                                    Answer
1                          Here are 2 answers                B
2                          Here is a single answer         True

Below is what it should look like

QuestionNo.    Question                                    Answer
1                          Here are 2 answers                B
2                          Here is a single answer         True
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account

The query outputs this:

QuestionNo.    Question                                    Answer (Answers are concatenated to each other)
1                          Here are 2 answers                BD
2                          Here is a single answer         True

So it seems query is working fine except difference is that except answers being on seperate rows, they are concatenated to each other as I have stated this in the query