How to calculate the difference in example below

I want to calculate difference between the “Total Marks” in a question, and the text inputs for each answer in a question. Also if a question has only one answer, then it should display the text input as a readonly and display the same value as the number under the “Total Mark” for that question in that text input.

Below is a screenshot which shows the table and the written problem I have with both scenarios:

Screenshot of table and information on problems

Below is the current jquery variable which should be doing the calculation and display the readonly textbox when necessary. But at the moment it is not working. Also at the moment I have display the number for total marks to be fixed as “5″ when doing the calculations, well this is incorrect as that it should be the number for within each row, so I believe the variable $sessionMarks that should be the number, not 5.

var questions = $('#markstbl td[class*="_ans"]').length-1;

//disable single entry
for (var i=0;i<=questions;i++){   
if($("[class*=q"+i+"_mark]").length ==1){

//find each question set and add listeners
for (var i=0;i<=questions;i++){                                     
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_'))
var t=0;
var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val());
var fixedno = 5;

Below is the code for the table, from the query it outputs the details of each question from the database and then it displays it in a table:


$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
   // 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();
    $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;



$previous_question_id = null;
$rowspans = array_count_values($searchQuestionId);
$output = "";
$questionid = 0; //whole question
$questionno = 0; //part of question
foreach ($searchQuestionContent as $key => $question) {
if ($previous_question_id != $searchQuestionId[$key]){

// removed logic, not necessary to set empty strings if you're skipping them
$output.= '<tr class="questiontd">' . PHP_EOL;
if ($previous_question_id != $searchQuestionId[$key]) {
$output.= '<td class="questionnumtd" name="numQuestion" rowspan="' . $rowspans[$searchQuestionId[$key]] . '">' . htmlspecialchars($searchQuestionId[$key]) . '</td>' . PHP_EOL;
$output.= '<td class="questioncontenttd q{$questionno++}_mark{$questionid}" rowspan="' . $rowspans[$searchQuestionId[$key]] . '">' . htmlspecialchars($question) . '</td>' . PHP_EOL;

$output.= '<td class="answertd" name="answers[]">';
$output.= $searchAnswer[$key];
$output.= '</td>';
$output.= '<td class="answermarkstd"><input class="individualMarks" q_group="1" name="answerMarks[]" id="individualtext" type="text" /></td>' . PHP_EOL;

if ($previous_question_id != $searchQuestionId[$key]) {
$output.= '<td class="noofmarkstd  q{$questionid++}_ans" q_group="1" 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 $output;


In the sample HTML which is in this fiddle, I can get it to work (except I want the calculation to be total marks minus number entered in text input, not what it is doing now which is when I enter a number in text input it performs an addition from 0 to whatever number entered in text input), but when I tried to edit the code above to do the same thing, then it doesn’t work, nothing happens.

The sample html is below:

<form id="Marks" action="/u0867587/Mobile_app/individualmarks.php" method="post">

<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>
<th class='noofmarksth'>Total Marks</th>

<tr class="questiontd">
<td class="questionnumtd" name="numQuestion" rowspan="3">1</td>
<td class="questioncontenttd" rowspan="3">Name three features in a ROM</td>
<td class="answertd" name="answers[]">A</td>
<td class="answermarkstd">
<input class="individualMarks q0_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
<td class="noofmarkstd q0_ans"  q_group="1" rowspan="3">5</td>
<tr class="questiontd">
<td class="answertd" name="answers[]">B</td>
<td class="answermarkstd">
<input class="individualMarks q0_mark_1" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
<tr class="questiontd">
<td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q0_mark_2" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
<tr class="questiontd">
<td class="questionnumtd" name="numQuestion" rowspan="1">2</td>
<td class="questioncontenttd" rowspan="1">Here is a single answer</td>
<td class="answertd" name="answers[]">True</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
<td class="noofmarkstd q1_ans" q_group="1" rowspan="1">5</td>

Leave a Reply


Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.