This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a problem with my calculation answer.
What ever is entered in the text input for each answer in a
question, it will subtract the number under the "Total Marks" column
for that question with the numbers entered within the text input for
that question.
Example is below:
In the text inputs I entered in numbers 1, 1 and 2. The Total Narks under the Total Marks Remaining column was 5 but it is now 1 as 5 - 1 - 1 - 2 = 1 Total Mark Remaining
The problem I have though is that if I change the top text input to lets say 3, then the calculation should now be 5 - 3 - 1 - 2 = -1 Total Mark Remaining. But it doesn't do this as that somewhere in the jquery function it has stated that if Total Marks Remaining number is less than 0, then display the original Total Marks Remaining number which is 5. This is incorrect, it should just display the minus number so the user knows that they need to reduce 1 mark. How can I get the minus number to be displayed if the Total Marks Remaining number is less than 0?
Below is the code for the jquery function:
Jquery:
$(function () {
//alert("here");
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) {
var t_marks = $("[class*=q" + i + "_ans]").html();
//alert(t_marks);
$("[class*=q" + i + "_mark]").val(t_marks)
.attr("disabled", "disabled");
//$("[class*=q"+i+"_mark]").attr("disabled","disabled");
}
}
//find each question set and add listeners
for (var i = 0; i <= questions; i++) {
$('input[class*="q' + i + '"]').keyup(function () {
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q') + 1, cl.indexOf('_'));
var tot_marks = $(".q" + questionno + "_ans_org").val();
//alert(tot_marks);
var ans_t = 0;
$("[class*=q" + questionno + "_mark]").each(function () {
var num = (isNaN(parseInt($(this).val()))) ? 0 : parseInt($(this).val());
ans_t += parseInt(num);
});
ans_t = tot_marks - ans_t;
//alert(ans_t);
//var fixedno = tot_marks;
var ans = (parseInt(ans_t) < 0) ? tot_marks : ans_t;
$(".q" + questionno + "_ans").val(ans);
$(".q" + questionno + "_ans_text").html(ans);
});
}
});
Below is the dynamic HTML Table:
HTML:
<body>
<table border='1' id='markstbl'>
<thead>
<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 Remaining</th>
</tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionId);
$prev_ques = '';
foreach ($searchQuestionId as $key => $questionId) {
?>
<tr class="questiontd">
<?php
if ($questionId != $prev_ques) {
?>
<td class="questionnumtd" name="numQuestion" rowspan=
"<?php echo $row_span[$questionId]; ?>"><?php
echo $questionId;
?><input type="hidden" name="q<?php echo $questionId; ?>_ans_org" class=
"q<?php echo $questionId; ?>_ans_org" value=
"<?php echo $searchMarks[$key]; ?>" /><input type="hidden" name=
"q<?php echo $questionId; ?>_ans" class=
"q<?php echo $questionId; ?>_ans" value=
"<?php echo $searchMarks[$key]; ?>" /></td>
<td class="questioncontenttd" rowspan="<?php echo $row_span[$questionId]; ?>">
<?php
echo $searchQuestionContent[$key];
?></td><?php
}
?>
<td class="answertd" name="answers[]"><?php
echo $searchAnswer[$key];
?></td>
<td class="answermarkstd"><input class=
"individualMarks q<?php echo $questionId; ?>_mark_0" q_group="1" name=
"answerMarks[]" id="individualtext" type="text" /></td><?php
if ($questionId != $prev_ques) {
?>
<td class="noofmarkstd q<?php echo $questionId; ?>_ans_text" q_group="1"
rowspan="<?php echo $row_span[$questionId]; ?>"><?php
echo $searchMarks[$key];
?></td><?php
}
?>
</tr><?php
$prev_ques = $questionId;
}
?>
</tbody>
</table>
</body>
</html>
This line is the culprit:
var ans = (parseInt(ans_t) < 0) ? tot_marks : ans_t;
Change it to:
var ans = ans_t;
Related
How do I sum grouped values from a table? For example I will sum the name?
this values :
2.997,
7.497
return : result!
I already have a function which delivers the whole sum this works fine. But I have don't a clue how to solve this problem ![enter image description here][1]
here is my function for the whole sum!
function calculateSum() {
var sum = 0;
$(".overviewTaxes").each(function() {
var value = $(this).text();
// add only if the value is number
if(!isNaN(value) && value.length != 0) {
sum += parseFloat(value);
}
});
$( ".globalTaxSum" ).html(sum);
}
<tbody>
<?php foreach ($overviews as $overview): ?>
<tr>
<td >
<?php echo $overview["forename"] . " " . $overview["surname"]; ?>
</td>
<td>
<?php echo $overview["article"]; ?>
</td>
<td>
<?php echo $overview["articleprice"]; ?>
</td>
<td class="overviewTaxes">
<?php
$ps = $overview["articleprice"] * 0.3; //flat rate
echo round($ps, 3) ;
?><br/>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Thanks for help.
Best regards
Marc Kevin
I have a jsfiddle here
Please look at Question 2 in the jsfiddle table as that question contains only a single answer.
In the jquery function what I have tried to do it state that if a question only
contains 1 answer, then the text input under the Marks Per Answer
column for that answer should be readonly and display the same value
as the Total Marks Remaining for that question.
Also if you look in the jsfiddle, it states that under Total Marks Remaining column for that single row, it displays the number 5, it should display number 0. As that number 5 in the readonly text input minus the original number for total marks Remaining (which was 5) means that the number under Total Marks Remaining should be 0 for that row.
My question is that what do I need to include in the jquery function in the jsfiddle so that:
if a question has a single answer, that text input is readonly
displays the total marks number in the text input
displays 0 under "Total Marks Remaining" for that row as it should
of perform the calculation between the number in the read only text
input and the original Total Marks Remaining number.
Below is the code for the jquery function:
Jquery:
$(function () {
//alert("here");
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) {
var t_marks = $("[class*=q" + i + "_ans]").html();
//alert(t_marks);
$("[class*=q" + i + "_mark]").val(t_marks)
.attr("disabled", "disabled");
//$("[class*=q"+i+"_mark]").attr("disabled","disabled");
}
}
//find each question set and add listeners
for (var i = 0; i <= questions; i++) {
$('input[class*="q' + i + '"]').keyup(function () {
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q') + 1, cl.indexOf('_'));
var tot_marks = $(".q" + questionno + "_ans_org").val();
//alert(tot_marks);
var ans_t = 0;
$("[class*=q" + questionno + "_mark]").each(function () {
var num = (isNaN(parseInt($(this).val()))) ? 0 : parseInt($(this).val());
ans_t += parseInt(num);
});
ans_t = tot_marks - ans_t;
//alert(ans_t);
//var fixedno = tot_marks;
var ans = (parseInt(ans_t) < 0) ? tot_marks : ans_t;
$(".q" + questionno + "_ans").val(ans);
$(".q" + questionno + "_ans_text").html(ans);
});
}
});
Below is the dynamic HTML Table:
HTML:
<table border='1' id='markstbl'>
<thead>
<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='totalmarksth'>Total Marks</th>
<th class='emptyth'></th>
</tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionId);
$prev_ques = '';
foreach($searchQuestionId as $key=>$questionId){
?>
<tr class="questiontd">
<?php
if($questionId != $prev_ques){
?>
<td class="questionnumtd" name="numQuestion" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$questionId?> <input type="hidden" name="q<?php echo$questionId?>_ans_org" class="q<?php echo$questionId?>_ans_org" value="<?php echo$searchMarks[$key]?>"><input type="hidden" name="q<?php echo$questionId?>_ans" class="q<?php echo$questionId?>_ans" value="<?php echo$searchMarks[$key]?>"></td>
<td class="questioncontenttd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$searchQuestionContent[$key]?> </td>
<?php
}
?>
<td class="answertd" name="answers[]"><?php echo$searchAnswer[$key]?></td>
<td class="answermarkstd">
<input class="individualMarks q<?php echo$questionId?>_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<?php
if($questionId != $prev_ques){
?>
<td class="totalmarkstd" rowspan="<?php echo$row_span[$questionId]?>"><?php echo$totalMarks[$key]?></td>
<td class="noofmarkstd q<?php echo$questionId?>_ans_text" q_group="1" rowspan="<?php echo$row_span[$questionId]?>"><?php echo"<strong>Marks Remaining:<br/>".$searchMarks[$key]."</strong>"?></td>
<?php
}
?>
</tr>
<?php
$prev_ques = $questionId;
}
?>
</tbody>
</table>
Your disabling loop is only checking the first of the two questions; a classic out-by-one error. Try this:
var questions = $('#markstbl td[class*="_ans"]').length;
questions will now be 2 instead of 1 and both questions will be looped over.
I am trying to create dynamic html table but the problem is that it is not displaying any data in the table. I know the query is correct because I tested the query in sql and it outputs the data. The problem I have I am guessing is the dynamic html table itself. Below is the code:
JavaScript/JQuery:
//javascript below will perform calculation between adding numbers between text inputs per each question
//answer for each calculation per question is stored under "Total Marks Remaining" Column
/*If a question only has one answer, then the text input under the "Marks Per Answer" column becomes
read only and displays the same number as the total marks under the "Total Marks Remaining" column
for that question*/
$(function() {
//alert("here");
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){
var t_marks = $("[class*=q"+i+"_ans]").html();
//alert(t_marks);
$("[class*=q"+i+"_mark]").val(t_marks).attr("disabled","disabled");
//$("[class*=q"+i+"_mark]").attr("disabled","disabled");
}
}
//find each question set and add listeners
for (var i=0;i<=questions;i++){
$('input[class*="q'+i+'"]').keyup(function(){
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_'));
var tot_marks = $(".q"+questionno+"_ans_org").val();
//alert(tot_marks);
var ans_t=0;
$("[class*=q"+questionno+"_mark]").each(function(){
var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val());
ans_t+=parseInt(num);
});
ans_t=tot_marks-ans_t;
//alert(ans_t);
//var fixedno = tot_marks;
var ans = (parseInt(ans_t)<0)?tot_marks:ans_t;
$(".q"+questionno+"_ans").val(ans);
$(".q"+questionno+"_ans_text").html(ans);
});
}
});
</script>
PHP:
<?php
if (isset($_POST['id'])) {
$_SESSION['id'] = $_POST['id'];
}
$assessment = $_SESSION['id'];
include('connect.php');
$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;
}?>
HTML:
<form id="Marks" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table border='1' id='markstbl'>
<thead>
<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>
</thead>
<?php
$row_span = array_count_values($searchQuestionId);
$prev_ques = '';
foreach($searchQuestionId as $key=>$questionId){?>
<tbody>
<tr class="questiontd">
<?php
if($questionId != $prev_ques){?>
<td class="questionnumtd" name="numQuestion" rowspan="<?=$row_span[$questionId]?>"><?=$questionId?> <input type="hidden" name="q<?=$questionId?>_ans_org" class="q<?=$questionId?>_ans_org" value="<?=$searchMarks[$key]?>"><input type="hidden" name="q<?=$questionId?>_ans" class="q<?=$questionId?>_ans" value="<?=$searchMarks[$key]?>"></td>
<td class="questioncontenttd" rowspan="<?=$row_span[$questionId]?>"><?=$searchQuestionContent[$key]?> </td>
<?php
}else{?>
<td class="questionnumtd" name="numQuestion" ></td>
<td class="questioncontenttd" ></td>
<?php
}?>
<td class="answertd" name="answers[]"><?=$searchAnswer[$key]?></td>
<td class="answermarkstd">
<input class="individualMarks q<?=$questionId?>_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<?php
if($questionId != $prev_ques){?>
<td class="noofmarkstd q<?=$questionId?>_ans_text" q_group="1" rowspan="<?=$row_span[$questionId]?>"><?=$searchMarks[$key]?></td>
<?php
}else{?>
<td class="noofmarkstd" q_group="1"></td>
<?php
}?>
</tr>
<?php
$prev_ques = $questionId;
}?>
</tbody>
</table>
</form>
Below is the screenshot of what it is displaying:
Below is what the table should display (The Marks per Answer Column contains text inputs for each row)
Below is database design so you can see where the data is coming from:
Session Table: (Where the exam details is stored)
SessionId SessionName
1 AAA
Question Table: (Where questions for each exams are stored)
SessionId QuestionId QuestionContent Total Marks
1 1 Name three features in a ROM 5
1 2 Here is a single answer 5
Answer Table: (Stores answers for each question in each exam)
AnswerId(auto) SessionId QuestionId Answer
1 1 1 A
2 1 1 B
3 1 1 D
4 1 2 True
Individual_Answer Table: (Stores each individual mark for each individual answer)
AnswerId AnswerMarks
1 2
2 2
3 1
4 5
UPDATE:
Looking at my html code, why is it displaying the table like this below:
Check for missing php open tag <?php before include('connect.php');
Also avoid short tags like <?= and replace them with <?php echo
place <tbody> outside the foreach loop
foreach($searchQuestionId as $key=>$questionId){
?>
<tbody>
to
</thead>
<tbody>
....
....
foreach($searchQuestionId as $key=>$questionId){
?>
Unless there is some code that is not being displayed here, perhaps it's as simple as the $assessment variable not being set, so nothing is being sent to the query?
Also, to confirm that you are actually getting the results back AND binding them OK, I'd be doing a var_dump on the arrays like searchQuestionContent to make sure they have the content you expect in them, if not you know your problem is in the query / binding data. If they do have the content you expect, then you know the problem lies in the table output.
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:
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.
$(function(){
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){
$("[class*=q"+i+"_mark]").attr("disabled","disabled")
}
}
//find each question set and add listeners
for (var i=0;i<=questions;i++){
$('input[class*="q'+i+'"]').keyup(function(){
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_'))
var t=0;
$("[class*=q"+questionno+"_mark]").each(function(){
var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val());
t+=parseInt(num);
})
var fixedno = 5;
$(".q"+questionno+"_ans").text((t>fixedno)?fixedno:t);
})
}
})
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:
<?php
$assessment = $_SESSION['id'] . $sessionConcat;
include('connect.php');
$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;
}
?>
....
<tbody>
<tr>
<?php
$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]){
$questionno=0;
}
// 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;
?>
</tr>
</tbody>
Update:
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'>
<thead>
<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>
</thead>
<tbody>
<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>
<td class="noofmarkstd q0_ans" q_group="1" rowspan="3">5</td>
</tr>
<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" />
</td>
</tr>
<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" />
</td>
</tr>
<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>
<td class="noofmarkstd q1_ans" q_group="1" rowspan="1">5</td>
</tr>
</tbody>
</table>
</form>
Below is what the database tables looks like, (this will follow the same data as the ones in the screenshot)
Session Table: (Where the exam details is stored)
SessionId SessionName
1 AAA
Question Table: (Where questions for each exams are stored)
SessionId QuestionId QuestionContent Total Marks
1 1 Name three features in a ROM 5
1 2 Here is a single answer 5
Answer Table: (Stores answers for each question in each exam)
AnswerId(auto) SessionId QuestionId Answer
1 1 1 A
2 1 1 B
3 1 1 D
4 1 2 True
Individual_Answer Table: (Stores each individual mark for each individual answer)
AnswerId AnswerMarks
1 2
2 2
3 1
4 5
It is not good practice to get and put values from html elements using html() which somehow belong to calculation or integers values, Therefore always use hidden fields for this type of problems, like I have included two hidden fields one for original value and one for the final value and at the end you can get the total marks values easily from the hidden field.
The code is updated for dynamic data. Use your own code for queries, mine is a rough code.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
var questions = $('#markstbl td[class*="_ans"]').length;
//disable single entry
for (var i=1;i<=questions;i++){
if($("[class*=q"+i+"_marks]").length ==1){
var t_marks = $("[class*=q"+i+"_ans]").html();
alert(t_marks);
$("[class*=q"+i+"_marks]").val(t_marks).attr("disabled","disabled");
//$("[class*=q"+i+"_mark]").attr("disabled","disabled");
}
}
//find each question set and add listeners
for (var i=0;i<=questions;i++){
$('input[class*="q'+i+'"]').keyup(function(){
var cl = $(this).attr('class').split(" ")[1]
var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_'));
var tot_marks = $(".q"+questionno+"_ans_org").val();
var ans_t=0;
$("[class*=q"+questionno+"_marks]").each(function(){
var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val());
ans_t+=parseInt(num);
});
ans_t=tot_marks-ans_t;
var ans = (parseInt(ans_t)<0)?tot_marks:ans_t;
$(".q"+questionno+"_ans").val(ans);
$(".q"+questionno+"_ans_text").html(ans);
});
}
});
</script>
</head>
<body>
<form id="Marks" action="/u0867587/Mobile_app/individualmarks.php" method="post">
<?php
$ident = mysqli_connect('localhost','root','');
mysqli_select_db($ident,'testdata');
$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
ORDER BY q.QuestionId, an.Answer
";
$res = mysqli_query($ident,$query);
$searchQuestionId = array();
$searchQuestionContent = array();
$searchAnswer = array();
$searchMarks = array();
while ($row = mysqli_fetch_array($res)) {
$searchQuestionId[] = $row['QuestionId'];
$searchQuestionContent[] = $row['QuestionContent'];
$searchAnswer[] = $row['Answer'];
$searchMarks[] = $row['QuestionMarks'];
}
?>
<form id="Marks" action="/u0867587/Mobile_app/individualmarks.php" method="post">
<table border='1' id='markstbl'>
<thead>
<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>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionId);
$output = '';
$rowCount = 1;
$newQuest_id = true;
foreach($searchQuestionId as $key=>$questionId){
if($newQuest_id == true){
$output.= '<tr class="questiontd">';
$output.= '<td class="questionnumtd" name="numQuestion" rowspan="'.$row_span[$questionId].'">'.$questionId.' <input type="hidden" name="q'.$questionId.'_ans_org" class="q'.$questionId.'_ans_org" value="'.$searchMarks[$key].'"><input type="hidden" name="q'.$questionId.'_ans" class="q'.$questionId.'_ans" value="'.$searchMarks[$key].'"></td>';
$output.= '<td class="questioncontenttd" rowspan="'.$row_span[$questionId].'">'.$searchQuestionContent[$key].' </td>';
}
$output.= '<td class="answertd" name="answers[]">'.$searchAnswer[$key].'</td>';
$output.= '<td class="answermarkstd">';
$output.= '<input class="individualMarks q'.$questionId.'_marks" q_group="1" name="answerMarks[]" id="individualtext" type="text" />';
$output.= '</td>';
if($newQuest_id == true){
$output.= '<td class="noofmarkstd q'.$questionId.'_ans_text" q_group="1" rowspan="'.$row_span[$questionId].'">'.$searchMarks[$key].'</td>';
$newQuest_id = false;
}
$output.= '</tr>';
if($row_span[$questionId] == $rowCount){
$newQuest_id = true;
}
$rowCount++;
}
echo $output;
?>
</tbody>
</table>
</form>
</form>
</body>
</html>
I am trying to make a "td" disappear whenever the date occured before that date today (in the past). But what is happening is that only the first column is affected while the others aren't.
<script type="text/javascript">
//<![CDATA[
function vanish() {
downloadUrl("bckendcommmap.php", function(data) {
var xml = data.responseXML;
var markers50 = xml.documentElement.getElementsByTagName("marker50");
// Split timestamp into [ Y, M, D, h, m, s ]
for (var i = 0; i < markers50.length; i++) {
var newdate = markers50[0].getAttribute("date");
var trainid = markers50[0].getAttribute("trainingid");
var t = newdate.split(/[- :]/);
// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2]);
alert(d);
var rightNow = new Date();
var trainingDate = new Date(d);
if (rightNow < d ) {
document.getElementById("assess").innerHTML = ""; /* make it disappear */
}
else if (rightNow > d ) {
document.getElementById("edit").innerHTML = "";
}
// -> Wed Jun 09 2010 13:12:01 GMT+0100 (GMT Daylight Time)
}
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
//]]>
</script>
Here are parts of the table and the td:
<body onload="vanish();">
<table id="gradient-style" width="290" border="0" cellspacing="0" cellpadding="0" >
<thead>
<tr>
<td align="center"><strong> Training Name</strong></td>
<td align="center"><strong> Description</strong></td>
<td align="center"><strong> Location</strong></td>
<td align="center"><strong> Date</strong></td>
<td align="center"><strong> Time Start</strong></td>
<td align="center"><strong> Time End</strong></td>
<td align="center"><strong> Capacity</strong></td>
<td align="center" colspan="2"><strong>Actions</strong></td>
</tr>
</thead>
<?php
while($rows=mysql_fetch_array($get)){
?>
<tbody>
<tr>
<td><?php echo $rows['title']; ?></td>
<td><?php echo $rows['description']; ?></td>
<td><?php echo $rows['location']; ?></td>
<td><?php echo $rows['date']; ?></td>
<td><?php echo $rows['start']; ?></td>
<td><?php echo $rows['end']; ?></td>
<td><?php echo $rows['capacity']; ?></td>
**<td align="center"><div id="edit">Edit</div></td><td align="center"><div id="assess">Assess Training</div></td>**
</tr>
</tbody>
<?php
}
?>
</table>
</body>
This line is the td that I'm trying to vanish with respect to the date:
<td align="center"><div id="edit">Edit</div></td><td align="center"><div id="assess">Assess Training</div></td>
You are only hiding the div, if you want to hide td Try this:
**<td id="edit" align="center"><div>Edit</div></td><td id="assess" align="center"><div>Assess Training</div></td>**
And the html code is inside a while loop so you will have multiple div's with same id! You need to give them unique ids:
$i = 0;
while($rows=mysql_fetch_array($get)){
/* Code... */
<?php
$i = $i + 1;
}
?>
And
<div id="assess<?php echo $i ?>>
And in your javascript:
/* Code... */
if (rightNow < d ) {
var i = 0;
while(document.getElementById("assess" + i) != null) {
document.getElementById("assess").innerHTML = ""; /* make it disappear */
i++;
}
/* Code.../
It's a bit hard to tell what the resulting full HTML in the page looks like, but do you realize that you can only have one object in a web page with a given ID. So, you can't have multiple objects with id="assess". If that is what you're doing, then getElementById will likely only return the first one - though the exact behavior is undefined because it's not valid HTML.
Second, why not just use CSS to hide things rather than clearing the innerHTML. For example, you could use a class="assess" instead of the id="assess". And, then create a CSS rule like this:
.triggerHide .assess {display: none;}
Then, when you want to hide that all assess classes in the table, you simple add the class "triggerHide" to the table. Boom, all the objects with the class assess in the table will all hide.