Counting the top checkbox responses when responses are stored as comma separated string in mysql row (Google Form -> mysql)

Obviously this is not the best way to store them, and I’m not sure if you can do it in the query. But you probably want to just select that column:

SELECT `answer` FROM `responses` WHERE `answer` <> ''

Then explode and add to a flat array and count the values:

$ans = [];
while($rows = mysqli_fetch_assoc($result)) {
    $ans = array_merge($ans, explode(',', $rows['answer']));
}
$count = array_count_values($ans);

The $count array should then yield something like:

Array
(
    [A] => 3
    [B] => 2
)

Then if needed to sort descending:

arsort($count);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top