[ create a new paste ] login | about

Link: http://codepad.org/T15Ui0Md    [ raw code | output | fork | 7 comments ]

PHP, pasted on Sep 14:
<?php
// you might even consider making these numeric arrays instead of having string-keys. up to you.
$correctAnswers = array(
	"s1" => array(
		"q1" => 'a',
		"q2" => 'b'
	),
	"s2" => array(
		"q1" => 'c',
		"q2" => 'a',
		"q3" => 'a',
		"q4" => 'a'
	)
);

$Ss = array(
	"s1" => "About OM",
	"s2" => "About EHS"
);

$userAnswers = array(	// you would get these values via $_POST["answers"]
	"s1" => array(
		"q1" => 'a',
		"q2" => 'b'
	),
	"s2" => array(
		"q1" => 'c',
		"q2" => 'a',
		"q3" => 'b',
		"q4" => 'c'
	)
);

$totalValue = "";
$correct = $wrong = 0;
foreach ($correctAnswers as $s=>$array)	// looping through each "s" of the correct answers array
{
	$about = $Ss[$s];
	
	$questionNum = 1;
	foreach ($array as $q=>$answer)	// looping through each question for a particular "s"
	{
		// print the 'about' for this "s" and the question number with a leading zero if it's one digit
		$totalValue .= "{$about} | Question ". str_pad($questionNum, 2, "0", STR_PAD_LEFT) .": {$userAnswers[$s][$q]}    ";
		
		// after we print the 'about' for this "s", we don't want to print it again, but instead print the same number of spaces.
		if (strlen(trim($about)) != 0)
			$about = str_pad("", strlen($about));
		
		if ($userAnswers[$s][$q] == $answer)
		{
			$correct++;
			$totalValue .= "Correct\n";
		}
		else
		{
			$wrong++;
			$totalValue .= "Wrong - Correct answer was: {$answer}\n";
		}
		
		$questionNum++;
	}
	
	$totalValue .= "\n";
}

echo $totalValue;

$total = $correct + $wrong;
$percentage = floor($correct / $total * 100);

echo "Correct: {$correct}\n";
echo "Wrong: {$wrong}\n";
echo "Total: {$total}\n";
echo "Percentage Correct: {$percentage}%";
?>


Output:
1
2
3
4
5
6
7
8
9
10
11
12
About OM | Question 01: a    Correct
         | Question 02: b    Correct

About EHS | Question 01: c    Correct
          | Question 02: a    Correct
          | Question 03: b    Wrong - Correct answer was: a
          | Question 04: c    Wrong - Correct answer was: a

Correct: 4
Wrong: 2
Total: 6
Percentage Correct: 66%


Create a new paste based on this one


Comments:
posted by interfaithmc on Sep 14
Your variable:
$userAnswers = array( // you would get these values via $_POST["answers"]
"s1" => array(
"q1" => 'a',
"q2" => 'b'
),
"s2" => array(
"q1" => 'c',
"q2" => 'a',
"q3" => 'b',
"q4" => 'c'
)
);

Can be changed to:
$userAnswers = array( // you would get these values via $_POST["answers"]
"s1" => array(
"q1" => $_POST['S1Q1'],
"q2" => $_POST['S1Q2']
),
"s2" => array(
"q1" => $_POST['S2Q1'],
"q2" => $_POST['S2Q2'],
"q3" => $_POST['S2Q3'],
"q4" => $_POST['S2Q4']
)
);

reply
posted by Travesty3 on Sep 16
Yes, you could do that, but if you have control over the script that sends the data to this script, my suggestion is to change the way that data is sent, so that it is simply received in that format. So your post data would look like:

answers[s1][q1]=a&answers[s1][q2]=b&answers[s2][q1]=c&answers[s2][q2]=a&answers[s2][q3]=a&answers[s2][q4]=a

You can do this by naming your HTML elements like that. Or if you are calling the script via AJAX, just make your post data look like that. If you want to update your question with the code you use to send the data over, I will elaborate.
reply
posted by interfaithmc on Sep 20
I have the following as choices in the HTML file:
a) <input type="radio" name="S1Q1" value="a" /> True
b) <input type="radio" name="S1Q1" value="b" /> False

so $_POST['S1Q1'] would be the choice the user made.

How would I incorporate your suggestion?
reply
posted by Travesty3 on Sep 20
a) <input type="radio" name="answers[s1][q1]" value="a" /> True
b) <input type="radio" name="answers[s1][q1]" value="b" /> False
reply
posted by interfaithmc on Sep 20
If i just leave the code with my original reply:
"s1" => array(
"q1" => $_POST['S1Q1'],
"q2" => $_POST['S1Q2']
),
"s2" => array(
"q1" => $_POST['S2Q1'],
"q2" => $_POST['S2Q2'],
"q3" => $_POST['S2Q3'],
"q4" => $_POST['S2Q4']
)
);

Than I should be ok right? Because it's alot of questions and changing it would take a bit of time I don't have now.
reply
posted by Travesty3 on Sep 20
Yes, you can do that.

But you will lose some flexibility. For instance, with your method, if you add another question, you have to add it in the HTML file as well as in the PHP file. With my method, you would only have to add it in the HTML file and PHP would get it automatically.

Would it really take that long to do a find/replace? My normal editor is Notepad++, and I love it. In Notepad++ I would just record a macro (CTRL+SHIFT+R to start/stop recording, CTRL+SHIFT+P to playback) to do this for me.

If you use Notepad++, follow these steps EXACTLY. Don't press any other keys on the keyboard except EXACTLY what I indicate:

1. Place your cursor at the beginning of the questions.
2. Press CTRL+SHIFT+R to start recording a macro.
3. CTRL+F to find, make sure the checkbox for "Match case" is checked.
4. In the "Find what:" box, type ` name="S` (without single quotes, note the preceding space) and click "Find Next".
5. Close the search box. The match should be highlighted.
6. Type ` name="answers[s`.
7. Press the left arrow 4 times.
8. CTRL+F to find, search for `Q`.
9. Close the search box. The match should again be highlighted.
10. Type `][q`.
11. Press CTRL+Right Arrow to go to the end of the word, before the ending quote.
12. Type `]`.
13. Press CTRL+SHIFT+R to stop recording the macro.
14. Press CTRL+SHIFT+P to playback the macro. Make sure that it works correctly. If so, keep pressing CTRL+SHIFT+P until all questions have been changed.

This will make your long, tedious task incredibly easy. This does assume that you don't have any other HTML elements with a name beginning with a capital S. If you do, just watch carefully when playing the macro back and if it messes one of those up, just undo (CTRL+Z) and then move the cursor past that element and continue.

Hopefully that helps!
reply
posted by interfaithmc on Sep 20
<?php

$fname = trim(strip_tags(stripslashes($_POST['fName'])));
$lname = trim(strip_tags(stripslashes($_POST['lName'])));
$tdate = trim(strip_tags(stripslashes($_POST['tDate'])));
$ydept = trim(strip_tags(stripslashes($_POST['yDept'])));

// you might even consider making these numeric arrays instead of having string-keys. up to you.
$correctAnswers = array(
"s1" => array(
"q1" => 'a',
"q2" => 'a'
),
"s2" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'c',
"q4" => 'a'
),
"s3" => array(
"q1" => 'a',
"q2" => 'b',
"q3" => 'a',
"q4" => 'a',
"q5" => 'c'
),
"s4" => array(
"q1" => 'b',
"q2" => 'a',
"q3" => 'a',
"q4" => 'a'
),
"s5" => array(
"q1" => 'b',
"q2" => 'b',
"q3" => 'a',
"q4" => 'a',
"q5" => 'b'
),
"s6" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'a',
"q4" => 'a',
"q5" => 'a',
"q6" => 'a'
),
"s7" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'a',
"q4" => 'd',
"q5" => 'a',
"q6" => 'a'
),
"s8" => array(
"q1" => 'a',
"q2" => 'b',
"q3" => 'a',
"q4" => 'b',
"q5" => 'e'
),
"s9" => array(
"q1" => 'f',
"q2" => 'e',
"q3" => 'e'
),
"s10" => array(
"q1" => 'a',
"q2" => 'a'
),
"s11" => array(
"q1" => 'f',
"q2" => 'd',
"q3" => 'a',
"q4" => 'c',
"q5" => 'e',
"q6" => 'b'
),
"s12" => array(
"q1" => 'b',
"q2" => 'f',
"q3" => 'a'
),
"s13" => array(
"q1" => 'a',
"q2" => 'd',
"q3" => 'a'
),
"s14" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'a',
"q4" => 'a'
),
"s15" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'a',
"q4" => 'a'
),
"s16" => array(
"q1" => 'a',
"q2" => 'a'
),
"s17" => array(
"q1" => 'a',
"q2" => 'e',
"q3" => 'e'
),
"s18" => array(
"q1" => 'a',
"q2" => 'a'
),
"s19" => array(
"q1" => 'c',
"q2" => 'a',
"q3" => 'a'
),
"s20" => array(
"q1" => 'a',
"q2" => 'e',
"q3" => 'a'
),
"s21" => array(
"q1" => 'd'
),
"s22" => array(
"q1" => 'g',
"q2" => 'd'
),
"s23" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'b'
),
"s24" => array(
"q1" => 'a',
"q2" => 'd'
),
"s25" => array(
"q1" => 'a',
"q2" => 'c',
"q3" => 'a',
"q4" => 'a'
),
"s26" => array(
"q1" => 'f',
"q2" => 'b',
"q3" => 'b',
"q4" => 'a',
"q5" => 'b'
),
"s27" => array(
"q1" => 'a',
"q2" => 'a',
"q3" => 'a',
"q4" => 'b',
"q5" => 'c',
"q6" => 'e'
)
);

$Ss = array(
"s1" => "About Our Mission",
"s2" => "About Employee Health Services",
"s3" => "About Our QA/PI Program",
"s4" => "About EMTALA",
"s5" => "About National Patient Safety Goals",
"s6" => "About Our Infection Control Program",
"s7" => "About Our Risk Management Program",
"s8" => "About Our Environment of Care Program",
"s9" => "About The Hazardous Material Spill Program",
"s10" => "About Our Interim Life Safety Measures",
"s11" => "About Emergency Management",
"s12" => "About Our Security Management Program",
"s13" => "About Our Patient Relations/Customer Service Program",
"s14" => "About Cultural Competency",
"s15" => "About Our Corporate Compliance Program",
"s16" => "About Fraud and Abuse",
"s17" => "About Ensuring Staff Competence",
"s18" => "About Staff Rights on Cultural, Ethical and Religious Beliefs",
"s19" => "About Sexual Harassment",
"s20" => "About Recognizing The Impaired Employee",
"s21" => "About The Employee Dress Code",
"s22" => "About The The Acceptable Behavior Guidelines",
"s23" => "Patient Rights",
"s24" => "About Our Bioethic Consultation Process",
"s25" => "About Abuse And Neglect",
"s26" => "About Our Privacy And Confidentiality: HIPAA",
"s27" => "Preventing Patient Falls"
);

$userAnswers = array( // you would get these values via $_POST["answers"]
"s1" => array(
"q1" => $_POST['S1Q1'],
"q2" => $_POST['S1Q2']
),
"s2" => array(
"q1" => $_POST['S2Q1'],
"q2" => $_POST['S2Q2'],
"q3" => $_POST['S2Q3'],
"q4" => $_POST['S2Q4']
),
"s3" => array(
"q1" => $_POST['S3Q1'],
"q2" => $_POST['S3Q2'],
"q3" => $_POST['S3Q3'],
"q4" => $_POST['S3Q4'],
"q5" => $_POST['S3Q5']
),
"s4" => array(
"q1" => $_POST['S4Q1'],
"q2" => $_POST['S4Q2'],
"q3" => $_POST['S4Q3'],
"q4" => $_POST['S4Q4']
),
"s5" => array(
"q1" => $_POST['S5Q1'],
"q2" => $_POST['S5Q2'],
"q3" => $_POST['S5Q3'],
"q4" => $_POST['S5Q4'],
"q5" => $_POST['S5Q5']
),
"s6" => array(
"q1" => $_POST['S6Q1'],
"q2" => $_POST['S6Q2'],
"q3" => $_POST['S6Q3'],
"q4" => $_POST['S6Q4'],
"q5" => $_POST['S6Q5'],
"q6" => $_POST['S6Q6']
),
"s7" => array(
"q1" => $_POST['S7Q1'],
"q2" => $_POST['S7Q2'],
"q3" => $_POST['S7Q3'],
"q4" => $_POST['S7Q4'],
"q5" => $_POST['S7Q5'],
"q6" => $_POST['S7Q6']
),
"s8" => array(
"q1" => $_POST['S8Q1'],
"q2" => $_POST['S8Q2'],
"q3" => $_POST['S8Q3'],
"q4" => $_POST['S8Q4'],
"q5" => $_POST['S8Q5']
),
"s9" => array(
"q1" => $_POST['S9Q1'],
"q2" => $_POST['S9Q2'],
"q3" => $_POST['S9Q3']
),
"s10" => array(
"q1" => $_POST['S10Q1'],
"q2" => $_POST['S10Q2']
),
"s11" => array(
"q1" => $_POST['S11Q1'],
"q2" => $_POST['S11Q2'],
"q3" => $_POST['S11Q3'],
"q4" => $_POST['S11Q4'],
"q5" => $_POST['S11Q5'],
"q6" => $_POST['S11Q6']
),
"s12" => array(
"q1" => $_POST['S12Q1'],
"q2" => $_POST['S12Q2'],
"q3" => $_POST['S12Q3']
),
"s13" => array(
"q1" => $_POST['S13Q1'],
"q2" => $_POST['S13Q2'],
"q3" => $_POST['S13Q3']
),
"s14" => array(
"q1" => $_POST['S14Q1'],
"q2" => $_POST['S14Q2'],
"q3" => $_POST['S14Q3'],
"q4" => $_POST['S14Q4']
),
"s15" => array(
"q1" => $_POST['S15Q1'],
"q2" => $_POST['S15Q2'],
"q3" => $_POST['S15Q3'],
"q4" => $_POST['S15Q4']
),
"s16" => array(
"q1" => $_POST['S16Q1'],
"q2" => $_POST['S16Q2']
),
"s17" => array(
"q1" => $_POST['S17Q1'],
"q2" => $_POST['S17Q2'],
"q3" => $_POST['S17Q3']
),
"s18" => array(
"q1" => $_POST['S18Q1'],
"q2" => $_POST['S18Q2']
),
"s19" => array(
"q1" => $_POST['S19Q1'],
"q2" => $_POST['S19Q2'],
"q3" => $_POST['S19Q3']
),
"s20" => array(
"q1" => $_POST['S20Q1'],
"q2" => $_POST['S20Q2'],
"q3" => $_POST['S20Q3']
),
"s21" => array(
"q1" => $_POST['S21Q1']
),
"s22" => array(
"q1" => $_POST['S22Q1'],
"q2" => $_POST['S22Q1']
),
"s23" => array(
"q1" => $_POST['S23Q1'],
"q2" => $_POST['S23Q2'],
"q3" => $_POST['S23Q3']
),
"s24" => array(
"q1" => $_POST['S24Q1'],
"q2" => $_POST['S24Q2']
),
"s25" => array(
"q1" => $_POST['S25Q1'],
"q2" => $_POST['S25Q2'],
"q3" => $_POST['S25Q3'],
"q4" => $_POST['S25Q4']
),
"s26" => array(
"q1" => $_POST['S26Q1'],
"q2" => $_POST['S26Q2'],
"q3" => $_POST['S26Q3'],
"q4" => $_POST['S26Q4'],
"q5" => $_POST['S26Q5']
),
"s27" => array(
"q1" => $_POST['S27Q1'],
"q2" => $_POST['S27Q2'],
"q3" => $_POST['S27Q3'],
"q4" => $_POST['S27Q4'],
"q5" => $_POST['S27Q5'],
"q6" => $_POST['S27Q6']
)
);

$totalValue = "";
$correct = $wrong = 0;

$totalValue = "First Name: " . $fname . "\n";
$totalValue .= "Last Name: " . $lname . "\n";
$totalValue .= "Date: " . $tdate . "\n";
$totalValue .= "Department: " . $ydept . "\n\n\n\n";

foreach ($correctAnswers as $s=>$array) // looping through each "s" of the correct answers array
{
$about = $Ss[$s];

$questionNum = 1;
foreach ($array as $q=>$answer) // looping through each question for a particular "s"
{
// print the 'about' for this "s" and the question number with a leading zero if it's one digit
$totalValue .= "{$about} | Question ". str_pad($questionNum, 2, "0", STR_PAD_LEFT) .": {$userAnswers[$s][$q]} ";

// after we print the 'about' for this "s", we don't want to print it again, but instead print the same number of spaces.
if (strlen(trim($about)) != 0)
$about = str_pad("", strlen($about));

if ($userAnswers[$s][$q] == $answer)
{
$correct++;
$totalValue .= "Correct\n";
}
else
{
$wrong++;
$totalValue .= "Wrong - Correct answer is: {$answer}\n";
}

$questionNum++;
}

$totalValue .= "\n";
}

// echo $totalValue; display the totalValue

$total = $correct + $wrong;
$percentage = floor($correct / $total * 100);

$totalValue .= "Correct: {$correct}\n";
$totalValue .= "Wrong: {$wrong}\n";
$totalValue .= "Total: {$total}\n";
$totalValue .= "Percentage Correct: {$percentage}%";

$check1 = substr($fname, 0, 1);
$userEmail = $check1 . $lname . "@test.org"; // get user email from first and last name

$header = "From: $userEmail";

$mailSubject = "Certificate of Attendance for: " . $lname . ", " . $fname . "";

$myEmail = "myemail@test.org";

mail ($myEmail, $mailSubject, $totalValue, $header);

?>
reply