1 回答
TA贡献1886条经验 获得超2个赞
因为所有的志愿者仇恨者(版主)都喜欢无缘无故地拒绝投票(除了引导他们的自负)......
这是我最终使用的最终结果。
然后我使用了一些 PHP 来做数学计算:
$statChecker_sql = "SELECT COUNT(*) as total,
(SELECT COUNT(*) FROM image_quiz WHERE answer = 'xx1')as answer1,
(SELECT COUNT(*) FROM image_quiz WHERE answer = 'xx2') as answer2,
(SELECT COUNT(*) FROM image_quiz WHERE answer = 'xx3') as answer3,
(SELECT COUNT(*) FROM image_quiz WHERE answer = 'xx4') as answer4,
(SELECT COUNT(*) FROM image_quiz WHERE answer = 'xx5') as answer5
FROM image_quiz;";
这是一个使用 PDO 的动态示例:
$statChecker_sql = "SELECT COUNT(*) as TOTAL, ";
for($i=0; $i<$totalPossibleAnswers; $i++){
if($i != ($totalPossibleAnswers - 1)){
$statChecker_sql .= "(SELECT COUNT(*) FROM image_quiz WHERE answer = :answerValue$i) as answer$i, ";
}else{
$statChecker_sql .= "(SELECT COUNT(*) FROM image_quiz WHERE answer = :answerValue$i) as answer$i "; //remove trailing comma (Im sure there is a better way)..lol!!!!!
}
}
$statChecker_sql .= " FROM image_quiz WHERE question_num = :targetQuestion";
$statChecker_stmt = $conn->prepare($statChecker_sql);
//dynamically add the potential values for parameterized query
for($i=0; $i<$totalPossibleAnswers; $i++){
$statChecker_stmt->bindValue(':answerValue'.$i, $quiz['question'][0]['answer'][$i]);}
$statChecker_stmt->bindValue(':targetQuestion',$targetQuestion);
$statChecker_stmt->execute();
$statChecker_stmt->setFetchMode(PDO::FETCH_ASSOC);
$statResults = $statChecker_stmt->fetch();
$statResultsCount = count($statResults); //current query index count
// submitted answer count * 100 \ total answers count
//output
echo '<br><br>Total Submitted Answers 1: ' . $statResults['TOTAL'] . '<br>';
echo 'Total Submitted Answers 2: ' . $statResultsCount . '<br>';
echo 'Detailed Answers breakdown: <br>';
for($z=1; $z<$statResultsCount; $z++){
echo $z . ' - ' . $possibleAnswerArray[$z-1] . ': ' . $statResults['answer'.($z-1)] . ' / ' . $statResults['TOTAL'] . ' (' . round(($statResults['answer'.($z-1)] * 100) / $statResults['TOTAL']) . '%)' . '<br>';
}
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报