У меня есть таблица, которая содержит команду Aspiring для определенных позиций и различные голоса. Ниже приведены данные
Teamtable | no | team | position | votes Cast | | 1 | A | President | 2 | | 3 | B | President | 1 | | 4 | C | Secretary | 2 | | 6 | D | Secretary | 1 |
Я хочу, чтобы получить это в формате html, используя php и mysql, как показано ниже
ОЖИДАЕМЫЙ ВЗГЛЯД В ФОРМАТЕ HTML И PHP
PRESIDENT Team | Total Votes | Percentage | A | 2 | 66.67 | B | 1 | 33.33 | SECRETARY Team | Total Votes | Percentage | C | 2 | 66.67 | D | 1 | 33.33 |
Это то, что я пробовал до сих пор
//QUERY $SQL=SELECT `team`, `position`, `votesCast` FROM Teamtable $Results=$db->query($SQL); $data=array(); while($row=mysqli_fetch_assoc($Results)){ $team=$row['team']; $position=$row['position']; $totalVote=$row['votesCast']; $data[$position][]=$position; $data1[$position][]=$team; $data2[$position][]=$totalVote; } foreach($data as $position =>$electionResults){ $teams=$data1[$position]; $totalVotes=$data2[$position]; foreach($teams as $re => $teas){ $votes=$totalVotes[$re]; echo " <table> <tr> <td>$teas</td> <td>$votes</td> </tr> </table>"; } }
,//QUERY $SQL=SELECT `team`, `position`, `votesCast` FROM Teamtable $Results=$db->query($SQL); $data=array(); while($row=mysqli_fetch_assoc($Results)){ $team=$row['team']; $position=$row['position']; $totalVote=$row['votesCast']; $data[$position][]=$position; $data1[$position][]=$team; $data2[$position][]=$totalVote; } foreach($data as $position =>$electionResults){ $teams=$data1[$position]; $totalVotes=$data2[$position]; foreach($teams as $re => $teas){ $votes=$totalVotes[$re]; echo " <table> <tr> <td>$teas</td> <td>$votes</td> </tr> </table>"; } }
в//QUERY $SQL=SELECT `team`, `position`, `votesCast` FROM Teamtable $Results=$db->query($SQL); $data=array(); while($row=mysqli_fetch_assoc($Results)){ $team=$row['team']; $position=$row['position']; $totalVote=$row['votesCast']; $data[$position][]=$position; $data1[$position][]=$team; $data2[$position][]=$totalVote; } foreach($data as $position =>$electionResults){ $teams=$data1[$position]; $totalVotes=$data2[$position]; foreach($teams as $re => $teas){ $votes=$totalVotes[$re]; echo " <table> <tr> <td>$teas</td> <td>$votes</td> </tr> </table>"; } }
Я должен пробовать до этого момента, любая помощь приветствуется.
Это может быть очень полезно для вас
while($row = mysqli_fetch_assoc($result)) { $data[$row['position']]['total'][]=$row['votes']; $data[$row['position']][]=$row; } foreach($data as $k=>$v){ echo '<p>'.$k.'</p>'; echo '<table border="1">'; $total_votes=array_sum($v['total']); foreach($v as $kk=>$vv){ if($kk!=='total'){ $percentage=round($vv['votes']/$total_votes*100,2); echo '<tr><td>'.$vv['tean'].'</td><td>'.$vv['votes'].'</td><td>'.$percentage.'%</td></tr>'; } } echo '</table>'; }
У тебя не будет стола на внешней стороне предпраздника.
echo "<table>"; foreach($teams as $re => $teas){ $votes=$totalVotes[$re]; echo "<tr> <td>$teas</td> <td>$votes</td> </tr>"; } echo "</table>";