Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Better way to do this

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
GB
I have a large form with many questions, the answers are either y (for yes), n (for no) or x (for N/A).

The form table is structured as

IPC1 | Question | radio for y, no or x
IPC2 | Question | radio for y, no or x
etc etc

RS1 | Question | radio for y, no or x
RS2 | Question | radio for y, no or x
etc etc

What I need to do is work out the percentage of yes's for all questions with IPC in the name that has either a yes or no response (completely ignore N/A).

I am doing this by updating counters for each question?

Code:
totalIPCCounter = 0
IPCYesCounter = 0

if ($_POST['IPC1'] == 'y') {
totalIPCCounter ++;
IPCYesCounter ++;
} elseif ($_POST['IPC1'] == 'n') {
totalIPCCounter ++;
}

if ($_POST['IPC2'] == 'y') {
totalIPCCounter ++;
IPCYesCounter ++;
} elseif ($_POST['IPC2'] == 'n') {
totalIPCCounter ++;
}

etc for all questions with IPC in the radio name. I repeat the above for all questions with RS in the radio name.

I then work out the percentages

Code:
IPCPercentage = (IPCYesCounter / totalIPCCounter) * 100

Is it possible to do this better as some sections have upto 30 questions which at 5 rows per question to work out the number of completed answers and also the percentage of yes's makes for a very large file.

Many thanks
 
This is one way of doing it.
There are others that don't involve looping either (at least not in the procedural code)
Code:
$numQuestions = 5; 
$yesAnswers = $noAnswers = 0;
$answers= array();
for ($i = 1; $i <= $numQuestions; $i++):
 if (isset($_POST['IPC'.$i])):
   $answers['IPC'.$i] = 'y';
   $yesAnswers++;
 else:
   $answers['IPC'.$i] = 'n';
   $noAnswers++;
endif;
echo '<pre>';
print_r($answers);
echo '<hr/>';
echo 'Percentage is ' . ($yesAnswers/$numQuestions) *100 .'%';
?>

another way of doing it is this

Code:
$numQuestions = 5;
$yesAnswers = count($_POST['IPC']);
echo 'Percentage is ' . ($yesAnswers/$numQuestions) *100 .'%';

for the second to work your questions would need to be like this
Code:
<input type="checkbox" value="1" name="IPC[[red]1[/red]]" /> Question 1<br/>
<input type="checkbox" value="1" name="IPC[[red]2[/red]]" /> Question 2<br/>
...etc
 
Thanks jpadie

I understand what is happening there, thank you.

I have two questions please

1. the percentage is calculated from not the total number of questions but the number of questions with either a yes or no answer. So if out of 20 total questions 8 were N/A, numQuestions should be 12.

2. Your loop will be perfect for the majority of the pages, one page however has letters in the question

i.e. RS1a, RS1b, RS2a, RS2b, RS2c, RS3a etc

Is this one step too far for a loop?
 
I see.

I had assumed them to be checkboxes but I see now that they are radio groups. I assume that you predefine one of the radios in each group as checked so that something in the group is always submitted?

in the case that all of your controls are called this

Code:
<div>
<input type="radio" name="IPC[[red]1[/red]]" value="Y" /> Y <br/>
<input type="radio" name="IPC[[red]1[/red]]" value="N" /> N <br/>
<input type="radio" name="IPC[[red]1[/red]]" value="N/A" /> N/A 
</div>

<div>
<input type="radio" name="IPC[[red]2[/red]]" value="Y" /> Y <br/>
<input type="radio" name="IPC[[red]2[/red]]" value="N" /> N <br/>
<input type="radio" name="IPC[[red]2[/red]]" value="N/A" /> N/A <br/>
</div>
you could use code like this

Code:
<?php
$percentage = 100 * (  
					count(  
						array_keys($_POST['IPC'], 'Y') 
						) 
					/ 
					( 
						count( 
							array_keys($_POST['IPC'], 'Y') 
							) 
						+ 
						count( 
							array_keys($_POST['IPC'], 'N') 
							) 
					)
					); 
?>


for the paper with the non-uniform question nomenclature, you need to keep a map of the field names in an array and loop them. Unless there is a definable pattern that is close to uniform, deriving a rule for processing them is unlikely to be sufficiently faster than just looping over them. However you could always call the questions something nice on screen, but in the actual HTML use a simple nomenclature like question[1].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top