1

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:

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

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

 Question             Answer        Marks Per Answer 
 What is 2+2?         B             (text input)
 Name the 3 Hobbits?  B             (text input)                   
                      C             (text input)
                      E             (text input)
  1. 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.
  2. 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;

include('connect.php');

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

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


        // 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()) {
        $searchQuestionContent[] = $dbQuestionId;
        $searchQuestionContent[] = $dbQuestionContent;
        $searchAnswer[] = $dbAnswer;
        }   

        ?>      

        </head>

        <body>


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

        <?php 

        echo "<table border='1' id='markstbl'>
        <tr>
        <th class='questionth'>Question</th>
        <th class='answerth'>Answer</th>
        <th class='answermarksth'>Marks per Answer</th>
        </tr>\n";

        foreach ($searchQuestionContent as $key=>$question) {
        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;

        ?>

        </form>

        </body>

Below is what Question Table looks like:

Question Table:

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

Answer Table:

AnswerId (auto)  QuestionId   Answer
1                 1           B   
2                 2           B
3                 2           C
4                 2           E
CMB
  • 37
  • 2
  • 7
  • A little more information about your database schema would be helpful. Also, where are you using the bound parameter `"s"`? Did you include your whole query? – slashingweapon Nov 03 '12 at 17:20
  • Let me just edit the code and question slightly so it will be easier for you and others give me 10 mins – CMB Nov 03 '12 at 17:28
  • @slashingweapon and everybody else, question has been updated to included updated code, updated examples and example of what the database tables looks like – CMB Nov 03 '12 at 17:42
  • What are you doing with `htmlspecialchars` – Sami Nov 03 '12 at 18:51
  • @Sami I did my research [here](http://stackoverflow.com/questions/4882307/when-to-use-htmlspecialchars-function) and it states that if I am echoing data from the database into HTML, then use htmlspecialchar so that it does not escape the HTML – CMB Nov 03 '12 at 18:58
  • Sorry I could not get much. I think `$searchAnswer` contains multiple values or only one value? As you are echoing it once. I have made answer for you. But that is very simple. I dont see any problem but with use of htmlspeicalchars my answer does not seeming me very correct – Sami Nov 03 '12 at 19:04

1 Answers1

0

It looks like you can just get rid of the grouping clause. You might want to order by question id, so all the answers for the same question are given together.

SELECT q.QuestionId, q.QuestionContent, an.Answer
FROM Question q
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
slashingweapon
  • 11,007
  • 4
  • 31
  • 50