More efficient way to do this? form data & counts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

More efficient way to do this? form data & counts

Post by Sinemacula »

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:

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;
}
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
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: More efficient way to do this? form data & counts

Post by Stryks »

Well ... if I understand you correctly, you just want to know how many of a given letter is clicked, regardless of which column it comes from?

If so, then something like ...

Code: Select all

<p><input type="radio" name="answer_list[1]" value="A"/>A - B<input type="radio" name="answer_list[1]" value="B"/></p>
<p><input type="radio" name="answer_list[2]" value="C"/>C - D<input type="radio" name="answer_list[2]" value="D"/></p>
etc.
Should display it.

When submitted, you should then have access to $_POST['answer_list'] ... allowing you to do something like ...

Code: Select all

if(isset($_POST['answer_list'])) {
   $ia = count(array_keys($_POST['answer_list'], 'A'));
   $ib = count(array_keys($_POST['answer_list'], 'B'));
 
   echo "<p>Found A $ib Times</p>";
}
I haven't tested that or anything, but I think it should work.

You could perhaps take the automation a little further if the list of questions is static by having the checkboxes dynamically built and then just creating the found list by searching for the options shown. Or you could even just pass array_unique() across $_POST['answer_list'] and then loop the results, testing for each and using the search string as a result key.

Code: Select all

$results = array_unique($_POST['answer_list']);
foreach($results as $result) $found[$result] = count(array_keys($_POST['answer_list'], $result));
print_r($found);
Something like that anyhow. Hope this helps. :)
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

Stryks wrote:Well ... if I understand you correctly, you just want to know how many of a given letter is clicked, regardless of which column it comes from?
Actually, no... I do need to know things on a column by column basis (each column represents a separate "topic").

As it turns out, I had posted another question that I didn't realize was pretty much related, so I did discover some ways to shorten this (although it appears I'm not completely understanding how to use arrays - but what I've done does work :D )

That thread is here: viewtopic.php?f=1&t=79033

And the code relevant to this question has become:

Code: Select all

//get the topics
$topic = array($_POST['topic1'],$_POST['topic2'],$_POST['topic3'],$_POST['topic4'],$_POST['topic5']);
 
//get the raw data for each choice
$ab = array($ab[$i],$ab[$i],$_POST['ab3'],$_POST['ab4'],$_POST['ab5']);
$cd = array($cd[$i],$cd[$a],$_POST['cd3'],$_POST['cd4'],$_POST['cd5']);
$ae = array($ae[$i],$ae[$a],$_POST['ae3'],$_POST['ae4'],$_POST['ae5']);
$bd = array($bd[$i],$bd[$a],$_POST['bd3'],$_POST['bd4'],$_POST['bd5']);
$ec = array($ec[$i],$ec[$a],$_POST['ec3'],$_POST['ec4'],$_POST['ec5']);
$da = array($da[$i],$da[$a],$_POST['da3'],$_POST['da4'],$_POST['da5']);
$eb = array($eb[$i],$eb[$a],$_POST['eb3'],$_POST['eb4'],$_POST['eb5']);
$ca = array($ca[$i],$ca[$a],$_POST['ca3'],$_POST['ca4'],$_POST['ca5']);
$de = array($de[$i],$de[$a],$_POST['de3'],$_POST['de4'],$_POST['de5']);
$bc = array($bc[$i],$bc[$a],$_POST['bc3'],$_POST['bc4'],$_POST['bc5']);
 
//start looping - need to loop 5 times to get through the 5 topics
for($i=0; $i<=4; $i++){
 
//GENERATE PICTURE COUNTS
 
//get count for image a
$ia[$i] = 0;
 
if($ab[$i] == 1){
    $ia[$i] = $ia[$i] + 1;
}
if($ae[$i] == 1){
    $ia[$i] = $ia[$i] + 1;
}
if($da[$i] == 2){
    $ia[$i] = $ia[$i] + 1;
}
if($ca[$i] == 2){
    $ia[$i] = $ia[$i] + 1;
}
 
//get count of image b
$ib[$i] = 0;
 
if($ab[$i] == 2){
    $ib[$i] = $ib[$i] + 1;
}
if($bd[$i] == 1){
    $ib[$i] = $ib[$i] + 1;
}
if($eb[$i] == 2){
    $ib[$i] = $ib[$i] + 1;
}
if($bc[$i] == 1){
    $ib[$i] = $ib[$i] + 1;
}
 
//get count of image c
$ic[$i] = 0;
 
if($cd[$i] == 1){
    $ic[$i] = $ic[$i] + 1;
}
if($ec[$i] == 2){
    $ic[$i] = $ic[$i] + 1;
}
if($ca[$i] == 1){
    $ic[$i] = $ic[$i]+ 1;
}
if($bc[$i] == 2){
    $ic[$i] = $ic[$i] + 1;
}
 
//get count of image d
$id[$i] = 0;
if($cd[$i] == 2){
    $id[$i] = $id[$i] + 1;
}
if($bd[$i] == 2){
    $id[$i] = $id[$i] + 1;
}
if($da[$i] == 1){
    $id[$i] = $id[$i] + 1;
}
if($de[$i] == 1){
    $id[$i] = $id[$i] + 1;
}
 
//get count of image e
$ie[$i] = 0;
 
if($ae[$i] == 2){
    $ie[$i] = $ie[$i] + 1;
}
if($ec[$i] == 1){
    $ie[$i] = $ie[$i] + 1;
}
if($eb[$i] == 1){
    $ie[$i] = $ie[$i] + 1;
}
if($de[$i] == 2){
    $ie[$i] = $ie[$i] + 1;
}
 
@arborint has suggested I consider moving a bunch of my code into functions and then calling it for each instance... but I'm still trying to figure out the whole arrays and loops thing! :D :wink:
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: More efficient way to do this? form data & counts

Post by Stryks »

Yeah, I just stumbled across that thread (actually I had looked at it and saw arborint had responded and decided there are some people I wouldn't presume to second guess). I then of course spotted this thread with no responses and jumped on in. :D

But now that I'm here ... my example is easily modified to multi column by just changing 'answer_list' to 'topic_1' / 'topic_2' / etc. Then to pull answers per column, you would just say

Code: Select all

$ia1 = count(array_keys($_POST['topic_1'], 'A'));
And from your example, in lines 4 through 14, shouldn't the first 2 values also be $_POST values, or are they coming from somewhere else?

Also, have you considered making your variable names a little more descriptive? That makes my head spin a little. 8O

Code: Select all

<p><input type="radio" name="topic_1[1]" value="A"/>A - B<input type="radio" name="topic_1[1]" value="B"/></p>
<p><input type="radio" name="topic_1[2]" value="C"/>C - D<input type="radio" name="topic_1[2]" value="D"/></p>
... to display, and ...

Code: Select all

 
if(isset($_POST['topic_1'])) {
   $ia1 = count(array_keys($_POST['topic_1'], 'A'));
   $ib1 = count(array_keys($_POST['topic_1'], 'B'));
 
   echo "<p>Found A $ib Times in Topic 1</p>";
}
Again, a tidier approach might be something like ...

Code: Select all

$results = array_unique($_POST['topic_1']);
foreach($results as $result) $found[1][$result] = count(array_keys($_POST['topic_1], $result));
$results = array_unique($_POST['topic_2']);
foreach($results as $result) $found[2][$result] = count(array_keys($_POST['topic_2], $result));
 
print_r($found[1]);  // Array of found options in topic 1
print_r($found[2]);  // Array of found options in topic 2
 
You could probably go even tidier with multidimensional form control arrays, but I've never tried that .. so I wont recommend it just now.

Seem any better?
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

Oops, that code wasn't right... that came off a bad "find/replace" -- should be:

Code: Select all

//get the raw data for each choice
$ab = array($_POST['ab1'],$_POST['ab2'],$_POST['ab3'],$_POST['ab4'],$_POST['ab5']);
$cd = array($_POST['cd1'],$_POST['cd2'],$_POST['cd3'],$_POST['cd4'],$_POST['cd5']);
$ae = array($_POST['ae1'],$_POST['ae2'],$_POST['ae3'],$_POST['ae4'],$_POST['ae5']);
$bd = array($_POST['bd1'],$_POST['bd2'],$_POST['bd3'],$_POST['bd4'],$_POST['bd5']);
$ec = array($_POST['ec1'],$_POST['ec2'],$_POST['ec3'],$_POST['ec4'],$_POST['ec5']);
$da = array($_POST['da1'],$_POST['da2'],$_POST['da3'],$_POST['da4'],$_POST['da5']);
$eb = array($_POST['eb1'],$_POST['eb2'],$_POST['eb3'],$_POST['eb4'],$_POST['eb5']);
$ca = array($_POST['ca1'],$_POST['ca2'],$_POST['ca3'],$_POST['ca4'],$_POST['ca5']);
$de = array($_POST['de1'],$_POST['de2'],$_POST['de3'],$_POST['de4'],$_POST['de5']);
$bc = array($_POST['bc1'],$_POST['bc2'],$_POST['bc3'],$_POST['bc4'],$_POST['bc5']);
 
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

Stryks wrote: Also, have you considered making your variable names a little more descriptive? That makes my head spin a little. 8O
Actually, for me those are pretty descriptive variable names - I'm coming from the conceptual side of the assessment rather than the code that makes it all work (being that my background is business and psychology rather than computers and coding). So, for me $ab1 is totally clear -- that's the choice between images a and b in topic 1... $ia1 is the count of image a in topic 1. :D

Of course, that seems to be making it more difficult from the coding side of things (particularly when asking for help! :wink: )

I'll look over your examples shortly... I'm sure I'll be back with more questions!

Thanks!
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: More efficient way to do this? form data & counts

Post by Stryks »

I totally get where you're at with the variable names, and I can see what they mean if I stop to look at them, but yeah .... if you're anything like me, you'll look back on this in 6 months and think .. "Well ... what the hell is that and where on earth did it come from".

But it's a small matter overall.

I just had a tinker, and came up with the following.

Code: Select all

<p>Samples for Topic 1</p>
<p><input type="radio" name="answer_list[1][1]" value="A"/>A - B<input type="radio" name="answer_list[1][1]" value="B"/></p>
<p><input type="radio" name="answer_list[1][2]" value="C"/>C - D<input type="radio" name="answer_list[1][2]" value="D"/></p>
<p>Samples for Topic 2</p>
<p><input type="radio" name="answer_list[2][1]" value="A"/>A - B<input type="radio" name="answer_list[2][1]" value="B"/></p>
<p><input type="radio" name="answer_list[2][2]" value="C"/>C - D<input type="radio" name="answer_list[2][2]" value="D"/></p>
So [1][1] is Topic 1, Question 1, etc.

To find the counts ...

Code: Select all

if(isset($_POST['answer_list'])) {
   foreach($_POST['answer_list'] as $topic_id=>$data) {
      $answers = array_unique($data);
      $foreach($answers as $answer) $found[$topic_id][$answer] = count(array_keys($data, $answer));
   }
}
So .. barring typo's in my sample there, you'll be able to access counts with the following ...

Code: Select all

echo "Topic 1 results for A = {$found[1]['A']}";
echo "Topic 2 results for C = {$found[2]['C']}";
 
Of course, doing it this way requires that you check that $found[x]['y'] exists before you use it, as only selected items are added to the $found array. You can overcome this if you know that all topics will have the same number of questions, and will all use the same values for tracking. Then instead of the array_unique() line, you would just do an array of possible values, eg. array('A', 'B', 'C', 'D');

Just another idea to kick around. 8)

EDIT: Just in case you didn't realise, that code to find the counts will return the results for as many questions and as many topics as you like. No more code needed.
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

That looks great... I didn't know about "$found"

There's a problem though... for reasons separate from the purpose of this particular code, the values of the variables have to remain "1" and "2" where a choice from the left side of the column is "1" and a choice from the right side of the column is "2"

That means that for a particular topic, half the time a choice of "A" would result in a value of "1" and half the time it would result in a value of "2" -- and the same thing for B, C, D & E.

Basically, in addition to producing the report, the data from this form is getting added to a larger databank from past research projects, all of which is scored and analyzed and run through some statistical stuff based on a 30+ year old excel spreadsheet that requires left choices to be "1" and right choices to be "2". That said, I would seriously love to update the whole process and have all data from here on out put into a mysql database, which ultimately would allow much better/easier data analysis -- but it's beyond my skill level (and time available to learn it, and budget to hire someone) to get into that now. So, for the time being, I think I'm stuck with it as is. (Although, as I write this, I'm thinking maybe it wouldn't be that difficult :? )

::Shudder:: I feel another all-niter coming on as I go OCD on this and try to figure it out! :lol:
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

Yep, I was right... couldn't let it go :lol:

I'm working with the code you suggested, and doing pretty well so far (after figuring out there was an extra "$" in it). :wink:

Anyway, I've got a new (related) question:

The form now has:

Code: Select all

<p><input type="radio" name="answer_list[1][1]" value="A"/>A - B<input type="radio" name="answer_list[1][1]" value="B"/></p>
<p><input type="radio" name="answer_list[1][2]" value="C"/>C - D<input type="radio" name="answer_list[1][2]" value="D"/></p>
<p><input type="radio" name="answer_list[1][3]" value="A"/>A - E<input type="radio" name="answer_list[1][3]" value="E"/></p>
...
<p><input type="radio" name="answer_list[5][8]" value="C"/>C - A<input type="radio" name="answer_list[5][8]" value="A"/></p>
<p><input type="radio" name="answer_list[5][9]" value="D"/>D - E<input type="radio" name="answer_list[5][9]" value="E"/></p>
<p><input type="radio" name="answer_list[5][10]" value="B"/>B - C<input type="radio" name="answer_list[5][10]" value="C"/></p>
 
I've got the epitomizing picture part (based on the counts) working.

Next is to get "same choice scores" - basically look at each row of a topic and comparing it to the same row of the other topics to see if the choice made was the same.
i.e.,
compare "answer_list[1][1]" with "answer_list[2][1]" and if they're the same, make $scs[1][2] = $scs[1][2]+1

Doing this the long way would be:

Code: Select all

 
$scs[1][2] = 0;
if($_POST['answer_list[1][1]'] == $_POST['answer_list[2][1]']) {
$scs[1][2] = $scs[1][2]+1;
}
if($_POST['answer_list[1][2]'] == $_POST['answer_list[2][2]']) {
$scs[1][2] = $scs[1][2]+1;
}
...
if($_POST['answer_list[4][10]'] == $_POST['answer_list[5][10]']) {
$scs[4][5] = $scs[4][5]+1;
}
So I thought I could make it shorter, by doing this:

Code: Select all

//start looping for all topics
for($i=1; $i<=4; $i++){
 
//check for same choices with topics 2 to 5
    for($a=2; $a<=5; $a++){
        $scs[$i][$a] = 0;
        for($b=1; $b<=10; $b++){
            if($_POST['answer_list[$i][$b]'] == $_POST['answer_list[$a][$b]']) {
            $scs[$i][$a] = $scs[$i][$a] + 1;
            }
        }
    }
}
But it doesn't seem to work (it's producing the wrong values, so something's wrong somewhere) - I'm guessing the problem is with the $_POST['answers_list[$i][$a]'] etc. -- I'm sure there is a way to use a

Code: Select all

foreach($_POST['answers_list'] $something as $something_else) {
type statement, but I'm not sure how to do that.

Thanks,
Scott
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: More efficient way to do this? form data & counts

Post by Stryks »

Ok, firstly, $found is just a variable ... it could just as easily be $wurble.

For understanding foreach, the manual is your friend.

I'd be interested to know how you are determining if a button selection came from the left or the right column of a topic. I know what I would have done, having started heading down the road I went, but you don't appear to have changed that. I can only imagine that you are comparing from the original question list?

As for the task you are currently working on, the way you have referenced the $_POST values isn't quite right.

Code: Select all

echo $_POST['answer_list[1][2]'];   // value not set
echo $_POST['answer_list'][1][2];   // happy days
Give it a go and let us know how it works out.
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

I did spend some time scouring the manual, but I think the fact that it was mid-all-niter might have hampered my full comprehension a bit. :oops: I got the sense that I could probably do a foreach to run through all the $_POST['answer_list'] values, but I ran into a wall - so I returned to my clumsy(?) looping $i approach.

Anyway, I actually did make a copy of the file in order to change the code in the form as you suggested:

Code: Select all

<p><input type="radio" name="answer_list[1][1]" value="A"/>A - B<input type="radio" name="answer_list[1][1]" value="B"/></p>
So, now when I say "left" or "right" side of the column, I guess what I really mean is:
  • does answer_list[1][1] equal the value for $answer_list[2][1] ?
  • does answer_list[1][2] equal the value for $answer_list[2][2] ?
  • ...
  • does answer_list[4][10] equal the value for $answer_list[5][10] ?
Which is basically the same as what I was looking for before, except that I always knew "left" and "right" because a value of "1" meant they'd chosen "left" and "2" meant they'd chosen "right" -- and I'm probably just stuck on the left/right thing because of the way the excel spreadsheet manages this data when it's all 1s and 2s instead of the letters. I should probably start figuring out how/if I can translate that to mysql/php so that I can really make this thing fully web-enabled (can a mysql/php app. handle basic stats like chi-square, sum-of-squares type stuff?)

Anyway, if I understand your correction regarding the $_POST values, I should be able to do something like this:

Code: Select all

 
//start looping for all topics
for($i=1; $i<=4; $i++){
 
//check for same choices with topics 2 to 5
    for($a=2; $a<=5; $a++){
        $scs[$i][$a] = 0;
        for($b=1; $b<=10; $b++){
            if($_POST['answer_list'][$i][$b] == $_POST['answer_list'][$a][$b]) {
            $scs[$i][$a] = $scs[$i][$a] + 1;
            }
        }
    }
}
 
Off to give it a try.

Thanks,
Scott
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

I decided to get it to print out what it's doing so I could figure out what I'm doing wrong...

I discovered that it's not actually comparing the values, but instead coming up with "values" for every instance of $_POST['answers_list'][$i][$b] and so on. :(

It was nice to see that it's looping properly, though - the $scs[$i][$a] values are exactly what they should be, given that every $_POST value in that statement is "values". :lol:

Back to the manual.
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

Perhaps I shoulda just gone to bed... turns out it was a silly mistake - the form says 'answer_list' and I put 'answers_list' in the script. :dubious: :oops:

Anyway, seems to be working now! :D
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Re: More efficient way to do this? form data & counts

Post by Sinemacula »

I've looked over the manual, and I'm having some difficulty really getting the "array" and "foreach" stuff - perhaps partly because I'm trying to relate it directly to what I'm doing here, and the interaction with the database (which is my next step - so far I've been just trying to go straight from form to output, so that I could make sure I can get the calculations and chart creation right. The next step will be having the data get put into a database along the way).

I've done a little playing around, so I see that this:

Code: Select all

$_POST['answer_list'] = array();
$_POST['answer_list'][0][0] = 1;
$_POST['answer_list'][0][1] = 2;
$_POST['answer_list'][0][2] = 1;
$_POST['answer_list'][1][0] = 2;
$_POST['answer_list'][1][1] = 2;
$_POST['answer_list'][1][2] = 1;
 
foreach ($_POST['answer_list'] as $answers) {
 
    foreach ($answers as $answer) {
        echo $answer.'<br />';
        }
}
Will produce a straight list of the values for answer_list -- looping through all the $_POST['answer_list'][0][] then the $_POST['answer_list'][1][]

What I haven't been able to figure out is how to get it to loop through the the second [] first - i.e., how to get it to loop through to produce a list in this order:
$_POST['answer_list'][0][0]
$_POST['answer_list'][1][0]
$_POST['answer_list'][0][1]
$_POST['answer_list'][1][1]
$_POST['answer_list'][0][2]
$_POST['answer_list'][1][2]

Which would then allow me to compare $_POST['answer_list'][][1] values by putting in a counter like

Code: Select all

if ($_POST['answer_list'][x][1] == $_POST['answer_list'][y][1]) {
$scs = $scs + 1;
}
I do have it working by using the $i=1; $i<=5; $i++ approach, but the sense I get is that using the foreach approach would be "cleaner" code (I don't know whether it would be more efficient).


Then there's the whole database issue (perhaps I should be starting a new topic in that forum??)

I'm sure the fact that my database knowledge is also limited will also be playing into my difficulties with the array concept.

For example, when I think of "array" when dealing with a data from the database, I think of multiple rows of a single field in the database table. The way I had originally conceptualized the database part of this project was a single table for all the response data, with a field for each variable in the form and a row/record representing a respondent's full submission. (Easy to design, and easy to get the data out to put into the existing excel spreadsheet that does more detailed & group level data analysis - all very 1 to 1.)

So, with a form that has fields like this:

Code: Select all

<p>Topic 1:<br /><input type="text" name="topic1" /><br />
L-Mode Rating: <select name="l1">
<option value="0" label="0" />0</option>
<option value="1" label="1" />1</option>
<option value="2" label="2" />2</option>
<option value="3" label="3" />3</option>
<option value="4" label="4" />4</option>
<option value="5" label="5" />5</option>
<option value="na" selected="selected" label="-" />-</option>
<option value="6" label="6" />6</option>
<option value="7" label="7" />7</option>
<option value="8" label="8" />8</option>
<option value="9" label="9" />9</option>
<option value="10" label="10" />10</option>
</select></p>
<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>
<td class="input">
<p>Topic 2:<br /><input type="text" name="topic2" /><br />
L-Mode Rating: <select name="l2">
<option value="0" label="0" />0</option>
<option value="1" label="1" />1</option>
<option value="2" label="2" />2</option>
<option value="3" label="3" />3</option>
<option value="4" label="4" />4</option>
<option value="5" label="5" />5</option>
<option value="na" selected="selected" label="-" />-</option>
<option value="6" label="6" />6</option>
<option value="7" label="7" />7</option>
<option value="8" label="8" />8</option>
<option value="9" label="9" />9</option>
<option value="10" label="10" />10</option>
</select></p>
<p><input type="radio" name="ab2" value="1"/>A - B<input type="radio" name="ab2" value="2"/></p>
<p><input type="radio" name="cd2" value="1"/>C - D<input type="radio" name="cd2" value="2"/></p>
<p><input type="radio" name="ae2" value="1"/>A - E<input type="radio" name="ae2" value="2"/></p>
<p><input type="radio" name="bd2" value="1"/>B - D<input type="radio" name="bd2" value="2"/></p>
<p><input type="radio" name="ec2" value="1"/>E - C<input type="radio" name="ec2" value="2"/></p>
<p><input type="radio" name="da2" value="1"/>D - A<input type="radio" name="da2" value="2"/></p>
<p><input type="radio" name="eb2" value="1"/>E - B<input type="radio" name="eb2" value="2"/></p>
<p><input type="radio" name="ca2" value="1"/>C - A<input type="radio" name="ca2" value="2"/></p>
<p><input type="radio" name="de2" value="1"/>D - E<input type="radio" name="de2" value="2"/></p>
<p><input type="radio" name="bc2" value="1"/>B - C<input type="radio" name="bc2" value="2"/></p>
</td>
...
 
I have a single database table with about 73 variables - one per input field in the table (that's 13 fields per topic, 5 topics, plus a few extras for name, userid, etc.).

Now, when I think of "array" in that context, I think of getting all values for $ae2 for example... which would actually be values of $ae2 for different form submission instances.

This leads me to think that if I want to use the $answer_list[][] approach instead, then I have to rethink my database design too... is that correct?

How would I design the database (and the queries), if I'm using this approach in the form?

Code: Select all

<p>Topic 1:<br /><input type="text" name="topic1" /><br />
L-Mode Rating: <select name="lmode[1]">
<option value="0" label="0" />0</option>
<option value="1" label="1" />1</option>
<option value="2" label="2" />2</option>
<option value="3" label="3" />3</option>
<option value="4" label="4" />4</option>
<option value="5" label="5" />5</option>
<option value="na" selected="selected" label="-" />-</option>
<option value="6" label="6" />6</option>
<option value="7" label="7" />7</option>
<option value="8" label="8" />8</option>
<option value="9" label="9" />9</option>
<option value="10" label="10" />10</option>
</select></p>
<p><input type="radio" name="answer_list[1][1]" value="A"/>A - B<input type="radio" name="answer_list[1][1]" value="B"/></p>
<p><input type="radio" name="answer_list[1][2]" value="C"/>C - D<input type="radio" name="answer_list[1][2]" value="D"/></p>
<p><input type="radio" name="answer_list[1][3]" value="A"/>A - E<input type="radio" name="answer_list[1][3]" value="E"/></p>
<p><input type="radio" name="answer_list[1][4]" value="B"/>B - D<input type="radio" name="answer_list[1][4]" value="D"/></p>
<p><input type="radio" name="answer_list[1][5]" value="E"/>E - C<input type="radio" name="answer_list[1][5]" value="C"/></p>
<p><input type="radio" name="answer_list[1][6]" value="D"/>D - A<input type="radio" name="answer_list[1][6]" value="A"/></p>
<p><input type="radio" name="answer_list[1][7]" value="E"/>E - B<input type="radio" name="answer_list[1][7]" value="B"/></p>
<p><input type="radio" name="answer_list[1][8]" value="C"/>C - A<input type="radio" name="answer_list[1][8]" value="A"/></p>
<p><input type="radio" name="answer_list[1][9]" value="D"/>D - E<input type="radio" name="answer_list[1][9]" value="E"/></p>
<p><input type="radio" name="answer_list[1][10]" value="B"/>B - C<input type="radio" name="answer_list[1][10]" value="C"/></p>
</td>
<td class="input">
<p>Topic 2:<br /><input type="text" name="topic2" /><br />
L-Mode Rating: <select name="lmode[2]">
<option value="0" label="0" />0</option>
<option value="1" label="1" />1</option>
<option value="2" label="2" />2</option>
<option value="3" label="3" />3</option>
<option value="4" label="4" />4</option>
<option value="5" label="5" />5</option>
<option value="na" selected="selected" label="-" />-</option>
<option value="6" label="6" />6</option>
<option value="7" label="7" />7</option>
<option value="8" label="8" />8</option>
<option value="9" label="9" />9</option>
<option value="10" label="10" />10</option>
</select></p>
<p><input type="radio" name="answer_list[2][1]" value="A"/>A - B<input type="radio" name="answer_list[2][1]" value="B"/></p>
<p><input type="radio" name="answer_list[2][2]" value="C"/>C - D<input type="radio" name="answer_list[2][2]" value="D"/></p>
<p><input type="radio" name="answer_list[2][3]" value="A"/>A - E<input type="radio" name="answer_list[2][3]" value="E"/></p>
<p><input type="radio" name="answer_list[2][4]" value="B"/>B - D<input type="radio" name="answer_list[2][4]" value="D"/></p>
<p><input type="radio" name="answer_list[2][5]" value="E"/>E - C<input type="radio" name="answer_list[2][5]" value="C"/></p>
<p><input type="radio" name="answer_list[2][6]" value="D"/>D - A<input type="radio" name="answer_list[2][6]" value="A"/></p>
<p><input type="radio" name="answer_list[2][7]" value="E"/>E - B<input type="radio" name="answer_list[2][7]" value="B"/></p>
<p><input type="radio" name="answer_list[2][8]" value="C"/>C - A<input type="radio" name="answer_list[2][8]" value="A"/></p>
<p><input type="radio" name="answer_list[2][9]" value="D"/>D - E<input type="radio" name="answer_list[2][9]" value="E"/></p>
<p><input type="radio" name="answer_list[2][10]" value="B"/>B - C<input type="radio" name="answer_list[2][10]" value="C"/></p>
</td>
I've certainly begun to see how this approach makes the scripting of the processing easier and cleaner, but I'm having difficulty following the implications for my database design.

My immediate thought was to just replace the $ab1 with $answer_list[1][1] in the same database structure, but something doesn't feel right about that.

I'm going to do some more manual reading, and some thinking, and playing... but I'll gladly consider any input/advice/suggestions too! :)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: More efficient way to do this? form data & counts

Post by Stryks »

I'm a bit confused about what it actually is you are trying to produce with your code.

I notice you have changed the display code from the option selected (A, B, C, etc.) to the numerical left or right identifiers. I was under the impression from the first post that you wanted to know the count of each value, not the count of lefts vs rights. It is possible to do both if required.

I am not really able to tell what you are hoping to do with the looping arrangement. Compare each question to the topic next to it? Or to all other rows perhaps?

Code: Select all

foreach ($_POST['answer_list'] as $topic_id=>$answers) {
    foreach ($answers as $answer_id=>$answer) {
        foreach ($_POST['answer_list'] as $scan_id=>$scan_data) {
            if ($topic_id != $scan_id) echo "Testing answer $answer_id from topic $topic_id with topic $scan_id<br />"; 
        }
    }
}
 


But yeah, I'm not sure of what output you wish to produce.

In regards to the database structure, I guess it's going to depend on what data you're going to store. I mean, are the topics static, or will they change? Do the questions mean anything, or is it just a matter of storing left or right? Are the questions different from topic to topic?

But for storing answers to 10 set questions for a static 5 topics for a specific user ... I'd have an tbl_answers table with 4 columns, user_id, topic_id, question_id, answer. I'd probably use the first three columns as a primary key.

That way you could pull all answers for a user, all answers for a topic (for a single user or all users), or all answers for a specific question across topics (for a single user or all users). You could even pull all users that answered a certain way.

For the static questions, I would probably store them in a separate table in named fields. You could give this static result set a unique primary key, and relate the tbl_answers data to that instead of the user_id (or as well as it).

Again, other approaches might be better than this one ... I'm still not really 100% on the overall purpose and requirements for the data.

Cheers
Post Reply