ログインシステムに基づいて投票できる PHP スクリプトがあります。
すべてのコードを MySQL ではなく PDO に切り替えようとしています。この特定のコードを切り替えるのに問題があります。ここにオリジナルがあります:
$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]
です。回答変数を指定した場合、配列からすべてを計算する場合でも、個々の回答結果を計算する場合でも。
私はここでゆるい終わりを迎えています。すぐに髪の毛がなくなるので、それが役に立ったら、どんな提案や批判も歓迎します。