我有一个 PHP 脚本,它允许基于登录系统进行投票。
我正在尝试将所有代码切换到 PDO 而不是 MySQL。我无法切换这段特定的代码。这是原文:
$get_votes = mysql_query("SELECT * FROM votes WHERE poll_id = 1 "); //select all votes to this poll
$votes = array(); //the array that will be containing all votes
$total_votes = 0;
while($vote = mysql_fetch_assoc($get_votes)) { //retrieve them
$answer_id = $vote['answer_id'];
$votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particular answer
$total_votes++;
}
$get_answers = mysql_query("SELECT * FROM answers WHERE poll_id = 1 ");
while($answer = mysql_fetch_assoc($get_answers)) { //loop through all answers
$id = $answer['id'];
$width = round((int)$votes[$id]/$total_votes*99+1); //100%
echo ' This Answer has ('.(int)$votes[$id].' vote'.((int)$votes[$id] != 1 ? "s":"").')
';
}
我想保留的是$width
票数和票数$votes[$id]
。如果我提供了答案变量,无论是从数组计算全部还是计算单个答案结果的情况。
我在这里无所适从,我很快就会没有头发,所以如果有帮助,欢迎任何建议/批评。