Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:36:37+00:00 2026-06-14T05:36:37+00:00

I have a question on how to display the correct table layout in my

  • 0

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) 
  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:

  <?php  
    $assessment = $_SESSION['id'] . $sessionConcat;
    include('connect.php');

    $query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, 
      GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer, 
    q.QuestionMarks 
    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
    $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()) {
    $searchQuestionId[] = $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 No.</th>
      <th class='questionth'>Question</th>
      <th class='answerth'>Answer</th>
      <th class='answermarksth'>Marks per Answer</th>
      <th class='noofmarksth'>Total Marks</th>
      </tr>\n";
      $previous_question_id = null;
      foreach ($searchQuestionContent as $key=>$question) {
        if ($previous_question_id == $searchQuestionId[$key]) {
      $searchQuestionId[$key] = '';
      $question = '';
  }else{
      $previous_question_id = $searchQuestionId[$key];
  }
        echo '<tr class="questiontd">'.PHP_EOL;
        echo '<td class="optiontypetd">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
        echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
        echo '<td class="answertd">';
        echo $searchAnswer[$key];
        echo '</td>' ;
        echo '<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" "/></td>' . PHP_EOL;
        echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
        echo "</table>" . PHP_EOL;

    ?>
    </form>
    </body>

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:36:39+00:00Added an answer on June 14, 2026 at 5:36 am

    Change GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer to Answer and try making this change in the foreach loop.

    $previous_question_id = null;
    foreach ($searchQuestionContent as $key=>$question) {
      if ($previous_question_id == $searchQuestionId[$key]) {
        $searchQuestionId[$key] = '';
        $question = '';
      } else {
        $previous_question_id = $searchQuestionId[$key];
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question on how to display the correct layout in my php
i have created random expression and want to link their answers to the keypad
I'm new with vim's taglist plugin and have a question regarding display style of
Very simple question... I have an array of pixels, how do I display them
I have Question and QuestionTypes. In Question table there is a foreign key that
I have question i.e how can i select a previous div CODE:- if($i >
I have added a segment control to my table view and what my problem/question
I'm currently creating a page in PHP that will display a user and their
I have got a table where i want to insert html in a td
Below is a link to my original question: PHP: How to display a variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.