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

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

Post by Sinemacula »

Ummm... I didn't change back to numerical left or right... the code is still the letters.


Yeah, it's a bit hard to explain exactly... if you want to try it out, you can access the prototype here: http://www.headgutcheck.com/head-gut-check-session - it does require registering on the site so that data can be added to the full research set - but if you let me know you're trying it out just to see what it is, I can delete your records afterwards.

Anyway, what's happening is the user views pairs of images, 10 pairs per topic, 1 pair presented at a time - and chooses one image or the other (left or right). The image pairs are the same for each topic, presented in the same order. The first three topics are changeable (by the user), the last two topics are static. (The prototype above is 4 changeable, 1 static)

The 10 pairs of images are actually 5 images shown in every possible pairing (each image shown 4 times, so it gets paired with each other image once/topic).

What I want to know is:
How many times was each image chosen for Topic 1?
How many times was each image chosen for Topic 2?
and so on...

So, for this question, getting the "count" of "A"s and "B"s etc. works perfectly -- and the value for each image is always between 0 and 4, and the total count of all images for a topic always adds to 10.

I also want to know:
How many times did you choose the same way for Topic 1 and Topic 2?
How many times did you choose the same way for Topic 2 and Topic 3?

Now, that second one is NOT did you choose Image A the same number of times for Topic 1 and Topic 2 (because of the way the pairings work, you could choose "A" twice for both topics, but at different choice points or questions )-- it's on a choice point by choice point basis... so it's actually:
Was your choice on the first pairing the same on Topic 1 and Topic 2?
Was your choice on the second pairing the same on Topic 1 and Topic 2?
... then total the number of times the choice was the same.

So for this question, I have to go through question by question for two topics, comparing values and adding a count of 1 for each same answer -- so the value will be between 0 and 10.

I'll test the code you suggested to see if it produces the desired output... if it does, great!! if not, I'm sure it will provide me with clues to help me either figure it out or to explain it better. :D

I've also started a new topic under the databases forum to ask about the database issue - rather than detail my thinking again here, here's a link:
viewtopic.php?f=2&t=79111

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 »

Sinemacula wrote:Ummm... I didn't change back to numerical left or right... the code is still the letters.
Yeah, I was scanning and kinda missed that. My bad. :roll:

I have to be honest, I'm still a little unsure if you want to compare topic 1 to topic 2 alone, or if you want to compare topic 1 to topic 3,4 and 5 as well. All the examples you give have been simplified to two, hence my confusion.

Assuming that (as all you examples seem to indicate) that you just want to compare a topic to the next topic only (apart from the last one which is skipped), then the following should give what you are after (I think :lol: ).

Code: Select all

$_POST['answer_list'][1][1] = 2;
$_POST['answer_list'][1][2] = 2;
$_POST['answer_list'][1][3] = 1;
$_POST['answer_list'][2][1] = 2;
$_POST['answer_list'][2][2] = 2;
$_POST['answer_list'][2][3] = 1;
$_POST['answer_list'][3][1] = 1;
$_POST['answer_list'][3][2] = 2;
$_POST['answer_list'][3][3] = 1; 
 
$max_scan = count($_POST['answer_list']);
 
foreach ($_POST['answer_list'] as $topic_id=>$answers) {
    $scan_topic = $topic_id + 1;
    if($scan_topic <= $max_scan) $scs[$topic_id][$scan_topic] = 0;
    foreach ($answers as $answer_id=>$answer) {
        // This answer matched the answer in the next topic - increment 
        if(($topic_id < $max_scan) && ($answer == $_POST['answer_list'][$scan_topic][$answer_id])) $scs[$topic_id][$scan_topic]++;
    }
}
 
echo "Similarity rating for topic 1 and 2 is {$scs[1][2]}<br />";
echo "Similarity rating for topic 2 and 3 is {$scs[2][3]}<br />";
 
If not, could you please give what the expected output would be for the example data I have included.

Cheers
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 »

Actually, I do need to get all topic to topic comparisons - I guess I didn't think about how simplifying for sake of the examples would also leave it unclear.

So, I need to know:

Code: Select all

 
echo "Similarity rating for topic 1 and 2 is {$scs[1][2]}<br />";
echo "Similarity rating for topic 1 and 3 is {$scs[1][3]}<br />";
echo "Similarity rating for topic 1 and 4 is {$scs[1][4]}<br />";
echo "Similarity rating for topic 1 and 5 is {$scs[1][5]}<br />";
echo "Similarity rating for topic 2 and 3 is {$scs[2][3]}<br />";
echo "Similarity rating for topic 2 and 4 is {$scs[2][4]}<br />";
echo "Similarity rating for topic 2 and 5 is {$scs[2][5]}<br />";
echo "Similarity rating for topic 3 and 4 is {$scs[3][4]}<br />";
echo "Similarity rating for topic 3 and 5 is {$scs[3][5]}<br />";
echo "Similarity rating for topic 4 and 5 is {$scs[4][5]}<br />";
However, I also need to separate them a bit in order to create different feedback text for different pairings...

Similarity between 1 & 2, 1 & 3, 2 & 3 get rated "similar", "neutral" or "dissimilar" depending on the similarity rating (same choice score).
Similarity between 1 & 4, 2 & 4, 3 & 4 get rated "identified", "neither identified nor dis-identified" or "dis-identified" depending on same choice score.
Similarity between 1 & 5, 2 & 5, 3 & 5, 4 & 5 get rated "positive", "neutral" or "negative" depending on same choice score.

So what I did was this:

Code: Select all

for($i=1; $i<=2; $i++){
    for($a=2; $a<=3; $a++){
 
if($scs[$i][$a] >= 6) {
    $similar[$i][$a] = "similar";
}
if($scs[$i][$a] < 6 and $scs[$i][$a] > 4) {
    $similar[$i][$a] = "neutral";
}
if($scs[$i][$a] <= 4) {
    $similar[$i][$a] = "dissimilar";
}
}
}
 
for($i=1; $i<=3; $i++){
 
$a = 4; 
 
if($scs[$i][$a] >= 6) {
    $similar[$i][$a] = "identified";
}
if($scs[$i][$a] < 6 and $scs[$i][$a] > 4) {
    $similar[$i][$a] = "neither identified nor dis-identified";
}
if($scs[$i][$a] <= 4) {
    $similar[$i][$a] = "dis-identified";
}
}
 
for($i=1; $i<=4; $i++){
 
$a = 5; 
 
if($scs[$i][$a] >= 6) {
    $similar[$i][$a] = "positive";
}
if($scs[$i][$a] < 6 and $scs[$i][$a] > 4) {
    $similar[$i][$a] = "neutral";
}
if($scs[$i][$a] <= 4) {
    $similar[$i][$a] = "negative";
}
}
 
...
 
echo '<p>You perceive <strong>'.$topic[1].'</strong> and <strong>'.$topic[2].'</strong> to be <strong><em>'.$similar[1][2].'</strong></em> to one another.</p>';
echo '<p>You perceive <strong>'.$topic[1].'</strong> and <strong>'.$topic[3].'</strong> to be <strong><em>'.$similar[1][3].'</strong></em> to one another.</p>';
echo '<p>You perceive <strong>'.$topic[2].'</strong> and <strong>'.$topic[3].'</strong> to be <strong><em>'.$similar[2][3].'</strong></em> to one another.</p>';
 
---
 
echo '<p>You are <strong>'.$similar[1][4].'</strong> with <strong>'.$topic[1].'</strong>.</p>';
echo '<p>You are <strong>'.$similar[2][4].'</strong> with <strong>'.$topic[2].'</strong>.</p>';
echo '<p>You are <strong>'.$similar[3][4].'</strong> with <strong>'.$topic[3].'</strong>.</p>';
 
...
 
echo '<p>Your attitude towards <strong>'.$topic[1].'</strong> is <strong>'.$similar[1][5].'</strong>.</p>';
echo '<p>Your attitude towards <strong>'.$topic[2].'</strong> is <strong>'.$similar[2][5].'</strong>.</p>';
echo '<p>Your attitude towards <strong>'.$topic[3].'</strong> is <strong>'.$similar[3][5].'</strong>.</p>';
echo '<p>Your attitude towards <strong>'.$topic[4].'</strong> is <strong>'.$similar[4][5].'</strong>.</p>';
I haven't had a chance yet to test your foreach code in place of my $i=1 loop... I'll get to that later tonight though.

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 »

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(($scan_id > $topic_id) && ($answer == $scan_data[$answer_id])) {
                if(!isset($scs[$topic_id][$scan_id])) $scs[$topic_id][$scan_id] = 1; else $scs[$topic_id][$scan_id]++;
            }
        }
    }
}
That should pull the results you are after, although it only tests for topics above each round. You could easily change it to compare against all results by changing from ($scan_id > $topic_id) to ($scan_id != $topic_id).

If I get a chance later on, I'll have a look at the feedback issue ... see what I can suggest.
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 »

This one works :)

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(($scan_id > $topic_id) && ($answer == $scan_data[$answer_id])) {
                if(!isset($scs[$topic_id][$scan_id])) $scs[$topic_id][$scan_id] = 1; else $scs[$topic_id][$scan_id]++;
            }
        }
    }
}
I tried the previous two also, and although they didn't result in what I was looking for, they were very instructive! Trying out the examples on the real data I'm using is very helpful in seeing the differences in code.
You could easily change it to compare against all results by changing from ($scan_id > $topic_id) to ($scan_id != $topic_id)
Yes, I saw that in the output of the one of the previous code sample you gave, a couple of posts ago - that would also work for my purpose, but would process more than I actually need.
Testing all topics above in each round actually does result in testing every topic pairing... having it compare all ends up with duplicates, since comparing question 1 topic 1 to question 1 topic 5 is the same as comparing question 1 topic 5 to question 1 topic 1 - i.e., $scs[1][5] is the ultimately the same value as $scs[5][1]


Thank you very much for your help and your code!

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 »

By the way - with your help, I've managed to reduce the size of this from my original version which had 1990 lines, down to 640 lines!! 8)
Post Reply