# How to create calculation in code below?

I have a table below:

Question No.       Question                    Answer       Marks per Answer       Total Marks
1                       Here are 2 answers        B                (text input)                    5
D                    (text input)
2                      Here is one answer        True            (text input)                   5

In the table above I have a text input which belongs to each answer. Now each question has its own amount of total marks. What I want to do is that if the user enters in a number in the text input, it should do a calculation on the difference between the number entered in the text input and the number under the "Total Marks" column. So if you look at the example below:

Question No.       Question                    Answer       Marks per Answer       Total Marks
1                       Here are 2 answers        B                (text input) = 2                    2
D                    (text input) = 1
2                      Here is one answer        True            (text input) = 5                   0

As you can see in the table above, the text inputs for the answers in question 1 equals 3 altogether. So 5 (from total marks for question 1) minus 3 = 2 (Total marks now equals 2)

For question 2, the text input for the answer in question 2  equals 5, so 5 (from total marks for question 2) minus 5 = 0 (Total marks now equals 0).

So my question is that how and what is the best method to use in order to perform these calculations?

Actually what I also want is that if a question only contains a single answer (like question 2 in the above example only has one answer), then I want the text input to become readonly and the text input should display the same number as the total marks as that it has to equal the same marks anyway, so it would look like this below:

Question No.   Question                  Answer     Marks per Answer             Total Marks
2                  Here is one answer    True       (text input(readonly)) = 5        0

Below is my code where it displays the tables and its contents:

``````    echo "<table border='1' id='markstbl'>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='noofmarksth'>Total Marks</th>
</tr>\n";
\$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>' ;
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];
}
}
echo '</tr>';
echo "</table>" . PHP_EOL;``````
Ray Paseur

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bruno1990

@ Ray

Here is the url: here

The data comes from the database, it comes from a query actually which is below:

``````  \$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
";``````

The sessionName is the name of the exam so if name of exam is "GHSWS" , then it looks for questions and answers the belong to GHSWS.

But I just need to know how to write the funtion so that if you type in a number in a text input that belongs to an answer in a question, it will calculate the difference from the Total marks for that question.

E,G Question 1 is worth 5 marks
Answers to Question 1:  "A" wroth 3 marks, "C" worth 2 marks
... it would be helpful to see the CREATE TABLE statements.
Also, the URL requires a login.