Page 1 of 1
Automatic code generator
Posted: Wed Aug 17, 2011 6:16 am
by naveendk.55
I am looking for the code that is not striking correctly. The scenario goes this way: There are a set of questions that are to be filled with the options Yes, No and NA. If the user chooses Yes then the score should be equal to the weightage of the question. In the same way it is vice versa for the selection NO. If the user chooses NA, then the question should not be scored. For Ex: If there are 10 questions that are to be awarded, and a question carries 10 points and the user selects Yes, then the user gets 10 points, if he selects No, then the score will be a Zero. If it is NA, then the question has to be ignored from the scoring. Overall, if there are 2 NA’s then the scoring should be only for 8 questions. If the user has 6 Yes and 2 No’s in the 8 questions, the score should read 60/80 and not 60/100.
Re: Automatic code generator
Posted: Wed Aug 17, 2011 7:22 am
by Dodon
it's not that hard to do just make a form and for each question make 3 radio buttons, yes/no/na.
Once the form is posted do something like:
Code: Select all
<?php
$question1 = $_post['question1'];
$question2 = $_post['question2'];
$numberofansweredquestions = 0;
$score = 0;
if ($question1 == "yes")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question1 == "no")
{
$numberofansweredquestions++;
}
if ($question2 == "yes")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question2 == "no")
{
$numberofansweredquestions++;
}
$maxscore = $numberofansweredquestions * 10;
echo "The score is: " . $score . "/" . $maxscore . "";
?>
I haven't tested above code but it gives you an idea what a possible solution might be, ofc there are nicer ways of doings this but since I expect you are a beginner in PHP I tried and keep it simple and easy to understand the logic behind it.
Re: Automatic code generator
Posted: Wed Aug 17, 2011 8:36 am
by naveendk.55
First I would like to thank you for replying and keeping the logic simple. However, I am developing a quality score sheet. The quality score sheet has 96 parameters (questions). I also want YES NO and NA as a drop down box (Which I can do it). Please let me know if there are any alternative ways to keep the code simple short. If you think above provided is the best way, then I'll go ahead with it. Thank you.
Re: Automatic code generator
Posted: Wed Aug 17, 2011 8:41 am
by Dodon
Are you grabbing the questions from a database? Or are you manually creating the form?
Re: Automatic code generator
Posted: Wed Aug 17, 2011 8:51 am
by Dodon
Either way, this might work:
Code: Select all
<?
if($_POST)
{
$numberofansweredquestions = 0;
$score = 0;
foreach($_POST as $question)
{
if ($question == "Yes")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question == "No")
{
$numberofansweredquestions++;
}
}
$maxscore = $numberofansweredquestions * 10;
echo "The score is: " . $score . "/" . $maxscore . "";
}
?>
it goes through all the fields that are posted from the form, if it's answered with yes or no it'll update the score.
Re: Automatic code generator
Posted: Wed Aug 17, 2011 9:03 am
by naveendk.55
Thank you.
I will work on this code and reply you if required any assistance. Thanks again.
Re: Automatic code generator
Posted: Thu Aug 18, 2011 6:54 am
by naveendk.55
Hi, I've created the below code. I'm getting the getting the data from a form. But the below code is showing total score as zero even after setting all the options to YES.
<?php
$question1 = $_REQUEST['Para_A_A1'];
$question2 = $_REQUEST['Para_A_A2'];
$question3 = $_REQUEST['Para_A_A3'];
$question4 = $_REQUEST['Para_A_A4'];
$question5 = $_REQUEST['Para_A_A5'];
$question6 = $_REQUEST['Para_A_A6'];
$numberofansweredquestions = 0;
$score = 0;
if ($question1 == "YES")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question1 == "NO")
{
$numberofansweredquestions++;
}
if ($question2 == "YES")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question2 == "NO")
{
$numberofansweredquestions++;
}
if ($question3 == "YES")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question3 == "NO")
{
$numberofansweredquestions++;
}
if ($question4 == "YES")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question4 == "NO")
{
$numberofansweredquestions++;
}
if ($question5 == "YES")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question5 == "NO")
{
$numberofansweredquestions++;
}
if ($question6 == "YES")
{
$numberofansweredquestions++;
$score += 10;
}
elseif ($question6 == "NO")
{
$numberofansweredquestions++;
}
$maxscore = $numberofansweredquestions * 10;
echo "The score is: " . $score . "/" . $maxscore . "";
?>
Re: Automatic code generator
Posted: Thu Aug 18, 2011 9:12 am
by naveendk.55
Dodon, your code is working. It appears that some issue with me form. I'll work further. I tested it manually assigning values from question1 to 6 and it worked properly.