请我尝试在每行的候选表中获取名为totalvotes的列的百分比,并显示所有列totalvotes的总和。如果他们的立场是一样的。现在我所取得的成就是我能够获得SUM TOTAL和百分比,但现在的问题是 页面上我显示的第一行总是100%,我不是这样 请这是我的代码
<?php
$counter=0;
$sql = mysqli_query($ret, "SELECT * From candidates WHERE position_id='$position_id'
ORDER BY candidates.position_id DESC,candidates.totalvotes DESC");
$row = mysqli_num_rows($sql);
$arr = array($row);
while ($row = mysqli_fetch_array($sql)){
//echo "". $row['campus_id'] ;
$total += $row['totalvotes'];
$perc =$row['totalvotes']*'100'/$total ;
$counter++;
echo "<tr class='odd pointer'>" ;
echo "<td class='a-center '>";
echo "<input type='checkbox' class='flat' name='table_records'>";
echo " </td>" ;
echo " <td >". $row['candidate_id']."</td>";
echo " <td >". $row['fullname']."</td>";
echo " <td >". $row['sex']."</td>";
echo "</td>" ;
echo " <td >". $row['totalvotes']."</td>";
echo " <td >".$perc."%</td>";
echo "";
echo " </td>";
echo "</tr>";
}
?>
答案 0 :(得分:0)
是你的回答让我知道:
<?php
$counter=0;
$sql = mysqli_query($ret, "SELECT * From candidates WHERE position_id='$position_id' ORDER BY candidates.position_id DESC, candidates.totalvotes DESC");
$row = mysqli_num_rows($sql);
$arr = array($row);
$totalvotes = 0;
while ($row = mysqli_fetch_array($sql)) {
$totalvotes += $row['totalvotes'];
}
while ($row = mysqli_fetch_array($sql)) {
//echo "". $row['campus_id'] ;
$perc = ($row['totalvotes']*'100'/$total);
echo "<tr class='odd pointer'>" ;
echo "<td class='a-center '><input type='checkbox' class='flat' name='table_records'></td>" ;
echo "<td>". $row['candidate_id']."</td>";
echo " <td>". $row['fullname']."</td>";
echo " <td>". $row['sex']."</td>";
echo " <td>". $row['totalvotes']."</td>";
echo " <td>".$perc."%</td>";
echo "</tr>";
$counter++;
}
?>