Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Display survey results to respondents in real time

I would like to show responses to a question to survey respondents in real time. This would behave like to a poll that shows you how many people picked each answer. I downloaded "Read Database" from the Question Library, but I'm not sure how to put the Perl script in. Thank you in advance for any insight.
asked Oct 7 by ChristineCarll (330 points)
I'm not sure how you want to display these totals, but here's an example of code you can use to count up how many respondents selected each of the first three options to a single select question:

# Do operations with @resps
my $resp1Count = 0;
my $resp2Count = 0;
my $resp3Count = 0;

foreach my $resp (@resps) {
    if ($resp == 1) {
        $resp1Count++;
    }
    elsif ($resp == 2) {
        $resp2Count++;
    }
    elsif ($resp == 3) {
        $resp3Count++;
    }
}

return $resp1Count . ',' . $resp2Count . ',' . $resp3Count;


Does that help?

1 Answer

0 votes
You can use below Perl Script to retrieve the number of respondents who selected a particular answer in question. I have considered a single select question named "S1" and retrieved the respondent's count who have answered 1st answer option.

        use DBI;
    my $db = '[REDACTED]';  #update database name
    my $host = '[REDACTED]';    #update database host name
    my $user = '[REDACTED]';       #update database username
    my $pass = '[REDACTED]';       #update database password

    my $dbh = DBI->connect("DBI:mysql:$db;host=$host",$user, $pass, { RaiseError => 1 } ) or
       die ( "Couldn't connect to database: " . DBI->errstr );

    my $Count;

    my $sth = $dbh->prepare("SELECT Count(*) FROM ".STUDYNAME()."_data1 WHERE S1='1'");   #update question name and response option number
    $sth->execute();
    my $row = $sth->fetchrow_arrayref();
     return $row->[0];
    $dbh->disconnect();
answered Oct 12 by KarthikMahankali Bronze (3,830 points)
edited Oct 12 by Zachary
Thanks for the response, Karthik.  It looks like you may have included real database credentials in your answer.  I'd like to think that no users on this forum would try to use this info maliciously, but I've decided it would probably still be best to edit your answer to remove this info.  Providing users with help on how to find their database credentials or an example of how their credentials will look is fine, but I would not recommend providing actual credentials to any database you might be using for your own data collection.
Thank you Zachary for doing this. I will keep a note of it. But somehow I have missed to remove those while submitting my answer.
...