0

特定の質問に投じられた投票数を見つけるために、テーブルエントリを(以前は)カウントするコードがあります。

ここに表があります:


  USER   |   answer_id  |  poll_id  |
------------------------------------
usename        5             1
user2          4             1
user3          5             1

等々。基本的に、ユーザーが投票するたびに、別の行が作成され、列にはユーザーが投票answer_idした質問番号が保持されます。

コードをsqlからPDOステートメントに更新した後、投票数を検索するコードは機能を停止し、最新の投票のみを検索するため、返される投票は1つだけになります。これが私が更新したコードです:



// Get Votes
$q = 'SELECT * FROM votes WHERE poll_id = :poll_id';
$params = array(':poll_id' => 1);
$stmt = $pdo->prepare($q);
$stmt->execute($params);
while ($row = $stmt->fetch()) {

$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )

$total_votes = 0;

$answer_id = $row['answer_id'];
$votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particulair answer
$total_votes++;
}

// End Get votes

ここから、これらの結果の処理が開始されます


// Start Get answers

$q = 'SELECT * FROM answers WHERE poll_id = :poll_id';
$params = array(':poll_id' => 1);
$stmt = $pdo->prepare($q);
$stmt->execute($params);
while ($row = $stmt->fetch()) { //loop through all answers this poll has

$id = $row['id']; //the id of the answer -> the amount of votes for each answer we stored in $votes[id_of_answer] so for this id it would be $votes[$id]
$width = round((int)$votes[$id]/$total_votes*99+1); //100% of votes

echo ''.$row['answer'].' 
('.(int)$votes[$id].' vote'.((int)$votes[$id] != 1 ? "s":"").')
'; }

そして、これがMySQLの元のコードです。


$get_votes = mysql_query("SELECT * FROM votes WHERE poll_id = '$id' "); //select all votes to this poll

$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )

$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++; }

//now loop through the answers and get their corresponding amount of votes from $votes[id_of_answer]

$get_answers = mysql_query("SELECT * FROM answers WHERE poll_id = '$id' ");

while($answer = mysql_fetch_assoc($get_answers)) { //loop through all answers this poll has

$id = $answer['id']; //the id of the answer -> the amount of votes for each answer we stored in $votes[id_of_answer] so for this id it would be $votes[$id] $width = round((int)$votes[$id]/$total_votes*99+1); // 100% of votes

echo ''.$answer['answer'].'
('.(int)$votes[$id].' vote'.((int)$votes[$id] != 1 ? "s":"").')
';

}

誰かが私がどこで間違っているのか教えてもらえますか?その//Get Votes部分は、iveが何をしたかを理解するのに苦労しています。私が見る限り、不当なことは何もありません。

4

2 に答える 2

1

fetchの代わりにbindparamfetchallをチェックしてください。

/* Execute a prepared statement by binding PHP variables */
$poll_id = 1;
$sth = $dbh->prepare('SELECT * FROM votes WHERE poll_id = :poll_id');
$sth->bindParam(':poll_id', $poll_id, PDO::PARAM_INT);
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
于 2013-02-27T18:52:24.993 に答える
1

配列を上書きして、ループでカウントします。

while ($row = $stmt->fetch()) {

  $votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )

  $total_votes = 0;

  $answer_id = $row['answer_id'];
  $votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particulair answer
  $total_votes++;
}

する必要があります:

$votes = array(); //the array that will be containing all votes ( we add them as we retrieve them in a while loop )
$total_votes = 0;

while ($row = $stmt->fetch()) {
  $answer_id = $row['answer_id'];
  $votes[$answer_id] = (int)$votes[$answer_id]+1; //add 1 vote for this particulair answer
  $total_votes++;
}
于 2013-02-27T19:01:22.427 に答える