PHP总票数和奖励额外积分给胜利者

时间:2014-04-02 02:41:44

标签: php

任何人都可以帮我提供代码吗?这是我得到的错误,"警告:implode():传递的参数无效"我希望输出打印总票数,包括给予获胜者的额外1分,最后打印选票结果,包括在表格中为获胜者提供的额外投票。

1|4
2|3
3|6
4|3
5|9

以下是表格和代码:

<form action="voting.php" method="get">

Choose your candidate:

<select name="candidate">

<option value=1 selected>1</option>        

<option value=2>2</option>

<option value=3>3</option>

<option value=4>4</option>

<option value=5>5</option>

</select>

<br>

<input type="submit" value="Send">                  

</form>



<?php
$inputs = file('results.txt');
$sum = 0 ;
$res = array();

foreach ($inputs as $line) {
  $var = explode('|', $line);
  $res[$var[0]] = $var[1];
  $add= $add +  $var[1];
}
$fin=array();
$maxVotes=max($res);
foreach ($res as $candidate => $voteCount) {
  if ($voteCount == $maxVotes) {
     $res[$candidate]++;
  $fin=$candidate . "|" . $res[$candidate];


  }
}
$final = implode("\n", $fin);
$fp = fopen('results.txt', 'w');
fwrite($fp, $final);
fclose($fp);
echo "Total votes casted: ",$add+1," votes.";
$file = 'results.txt';
readfile($file);
?>

1 个答案:

答案 0 :(得分:0)

我也猜测你想要将结果附加到fin数组而不是用字符串覆盖它,你可以用

来做

$fin[] = $candidate . "|" . $res[$candidate];