Automatic code generator
Moderator: General Moderators
-
naveendk.55
- Forum Newbie
- Posts: 24
- Joined: Tue Aug 16, 2011 10:13 am
Automatic code generator
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
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:
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.
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 . "";
?>
-
naveendk.55
- Forum Newbie
- Posts: 24
- Joined: Tue Aug 16, 2011 10:13 am
Re: Automatic code generator
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
Are you grabbing the questions from a database? Or are you manually creating the form?
Re: Automatic code generator
Either way, this might work:
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.
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 . "";
}
?>
-
naveendk.55
- Forum Newbie
- Posts: 24
- Joined: Tue Aug 16, 2011 10:13 am
Re: Automatic code generator
Thank you.
I will work on this code and reply you if required any assistance. Thanks again.
I will work on this code and reply you if required any assistance. Thanks again.
-
naveendk.55
- Forum Newbie
- Posts: 24
- Joined: Tue Aug 16, 2011 10:13 am
Re: Automatic code generator
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 . "";
?>
<?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 . "";
?>
-
naveendk.55
- Forum Newbie
- Posts: 24
- Joined: Tue Aug 16, 2011 10:13 am
Re: Automatic code generator
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.