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 8785857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:27:54+00:00 2026-06-13T21:27:54+00:00

I have 2 small questions I need help on which deals with the code

  • 0

I have 2 small questions I need help on which deals with the code below:

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 = '';
          $searchMarks[$key] = '';
      }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 '</tr>';
        echo "</table>" . PHP_EOL;

Question 1: If you look at the table above, I have a a column where it will contain text inputs. Now each text input will belong to each answer in the “Answer” column. My question is that is the text input set up correctly with me including a [] in the name attribute to set up an array? Should I include [] and is there anything else I should include in the text input code?

Question 2: At the moment the code above makes the table look like this example in JSFIDDLE1. But I want the table to be displayed as this example JSFIDDLE2. As you can see the blank rows in the other table have been rowspan so that it looks like the question and the total marks looks like its in one cell. How can I get the table above to look like JSFIDDLE2?

UPDATE:

Below is the full code, I am still receiving undefined index whenver $rowspans[.... is used. Is the code below correct:

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

$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, an.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 = ?
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();
$searchMarks = array();

// Fetch the results into an array

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

?>      

</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='noofmarksth'>Total Marks</th>
</tr>\n";
$previous_question_id = null;
$rowspans = array_count_values($searchQuestionId);
foreach ($searchQuestionContent as $key=>$question) {
if ($previous_question_id == $searchQuestionId[$key]) {
$searchQuestionId[$key] = '';
$question = '';
$searchMarks[$key] = '';
}else{
$previous_question_id = $searchQuestionId[$key];
}
echo '<tr class="questiontd">'.PHP_EOL;
echo '<td class="optiontypetd">'.htmlspecialchars($rowspans[$searchQuestionId[$key]]).'</td>' . PHP_EOL;
echo '<td>'.htmlspecialchars($question).'</td>' . PHP_EOL;
echo '<td class="answertd">';
echo $searchAnswer[$key];
echo '</td>';        echo '<td class="noofmarkstd">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
}
echo '</tr>';
echo "</table>" . PHP_EOL;

?>

<p><input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" /></p>

</form>
  • 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-13T21:27:55+00:00Added an answer on June 13, 2026 at 9:27 pm

    Q1. Yes, you could even specify a name between the [].

    Q2. It seems that $searchQuestionId is filled before your loop, right? In that case you could use array_count_values to store the frequency of each ID and access that array during the loop:

    before your loop: $rowspans = array_count_values($searchQuestionId);

    access rowspan value during loop with: $rowspans[$searchQuestionId[$key]].

    You’ll have to remove the line $searchQuestionId[$key] = ''; or else you won’t be able to access the correct $rowspan key.

    Finally, you’ll have to set up some more if statements to skip the <td>s on certain rows.

    $previous_question_id = null;
    $rowspans = array_count_values($searchQuestionId);
    foreach ($searchQuestionContent as $key=>$question) {
    
        // removed logic, not necessary to set empty strings if you're skipping them
    
        echo '<tr class="questiontd">'.PHP_EOL;
    
        if ($previous_question_id != $searchQuestionId[$key]) {
            echo '<td class="optiontypetd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchQuestionId[$key]).'</td>' . PHP_EOL;
            echo '<td rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.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;
    
        if ($previous_question_id != $searchQuestionId[$key]) {
            echo '<td class="noofmarkstd" rowspan="'.$rowspans[$searchQuestionId[$key]].'">'.htmlspecialchars($searchMarks[$key]).'</td>' . PHP_EOL;
        }
    
        // moved this to the end
        if ($previous_question_id != $searchQuestionId[$key]) {
            $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

Check below for UPDATE, I could reproduce and need help. I have a strange
I have spent several days now researching Hibernate and have several small questions about
I have two Questions. One small and one greater problem. Why i have to
I am going to be making a small user system but I have questions.
Just a small question regarding joins. I have a table with around 30 fields
I have a small-scale WPF application using VB.net as the code behind and I
How to obfuscate code quickly. I have a very small Java App and I
got a small little problem I'm hoping someone can help me with. I have
I have small question by entity framework 4.1. There is interface of image DTO:
I have small question. When I started to programm my application, I grouped types

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.