More efficient way to do this? form data & counts
Posted: Mon Feb 18, 2008 3:00 pm
Just wondering if someone would look at a portion of my form code and the processing code to let me know if there's a more efficient way to do what I need.
Basically, the form has 5 columns of paired radio buttons (each column representing a topic), like this:
Topic 1
a - b
c - d
a - e
b - d
e - c
d - a
e - b
c - a
d - e
b - c
And I want to count how many times each letter was selected for each topic.
So, here's the form html and the processor php snippets for one topic:
I then repeat that same php code for each topic, just replacing the "1" in the variable names with "2" and so on for each of the 5 topics.
Is there a more efficient way to achieve this? I was just thinking that I don't know much about looping, but I wondered if an array/loop situation would work here to make it more efficient?
Thanks,
Scott
Basically, the form has 5 columns of paired radio buttons (each column representing a topic), like this:
Topic 1
a - b
c - d
a - e
b - d
e - c
d - a
e - b
c - a
d - e
b - c
And I want to count how many times each letter was selected for each topic.
So, here's the form html and the processor php snippets for one topic:
Code: Select all
<p>Topic 1:<br /><input type="text" name="topic1" /><br />
<p><input type="radio" name="ab1" value="1"/>A - B<input type="radio" name="ab1" value="2"/></p>
<p><input type="radio" name="cd1" value="1"/>C - D<input type="radio" name="cd1" value="2"/></p>
<p><input type="radio" name="ae1" value="1"/>A - E<input type="radio" name="ae1" value="2"/></p>
<p><input type="radio" name="bd1" value="1"/>B - D<input type="radio" name="bd1" value="2"/></p>
<p><input type="radio" name="ec1" value="1"/>E - C<input type="radio" name="ec1" value="2"/></p>
<p><input type="radio" name="da1" value="1"/>D - A<input type="radio" name="da1" value="2"/></p>
<p><input type="radio" name="eb1" value="1"/>E - B<input type="radio" name="eb1" value="2"/></p>
<p><input type="radio" name="ca1" value="1"/>C - A<input type="radio" name="ca1" value="2"/></p>
<p><input type="radio" name="de1" value="1"/>D - E<input type="radio" name="de1" value="2"/></p>
<p><input type="radio" name="bc1" value="1"/>B - C<input type="radio" name="bc1" value="2"/></p>
</td>Code: Select all
//get count of image a
$ia1 = 0;
if($_POST['ab1'] == 1){
$ia1 = $ia1 + 1;
}
if($_POST['ae1'] == 1){
$ia1 = $ia1 + 1;
}
if($_POST['da1'] == 2){
$ia1 = $ia1 + 1;
}
if($_POST['ca1'] == 2){
$ia1 = $ia1 + 1;
}
//get count of image b
$ib1 = 0;
if($_POST['ab1'] == 2){
$ib1 = $ib1 + 1;
}
if($_POST['bd1'] == 1){
$ib1 = $ib1 + 1;
}
if($_POST['eb1'] == 2){
$ib1 = $ib1 + 1;
}
if($_POST['bc1'] == 1){
$ib1 = $ib1 + 1;
}
//get count of image c
$ic1 = 0;
if($_POST['cd1'] == 1){
$ic1 = $ic1 + 1;
}
if($_POST['ec1'] == 2){
$ic1 = $ic1 + 1;
}
if($_POST['ca1'] == 1){
$ic1 = $ic1 + 1;
}
if($_POST['bc1'] == 2){
$ic1 = $ic1 + 1;
}
//get count of image d
$id1 = 0;
if($_POST['cd1'] == 2){
$id1 = $id1 + 1;
}
if($_POST['bd1'] == 2){
$id1 = $id1 + 1;
}
if($_POST['da1'] == 1){
$id1 = $id1 + 1;
}
if($_POST['de1'] == 1){
$id1 = $id1 + 1;
}
//get count of image e
$ie1 = 0;
if($_POST['ae1'] == 2){
$ie1 = $ie1 + 1;
}
if($_POST['ec1'] == 1){
$ie1 = $ie1 + 1;
}
if($_POST['eb1'] == 1){
$ie1 = $ie1 + 1;
}
if($_POST['de1'] == 2){
$ie1 = $ie1 + 1;
}Is there a more efficient way to achieve this? I was just thinking that I don't know much about looping, but I wondered if an array/loop situation would work here to make it more efficient?
Thanks,
Scott