如何使用html和php以特定方式格式化数据 [英] How to format data in specific way using html and php
本文介绍了如何使用html和php以特定方式格式化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张桌子,里面包含了一些特殊职位和各种投票的有抱负的团队。
数据低于
Teamtable
|没有|团队|位置|投票|
| 1 | A |总统| 2 |
| 3 | B |总统| 1 |
| 4 | C |秘书| 2 |
| 6 | D |秘书| 1 |
我希望能够使用php和mysql以html格式获取它, p>
HTML和PHP格式的预期视图
PRESIDENT
团队|总票数|百分比|
A | 2 | 66.67 |
B | 1 | 33.33 |
SECRETARY
团队|总票数|百分比|
C | 2 | 66.67 |
D | 1 | 33.33 |
这是我到目前为止所尝试的结果
< pre $
// QUERY
$ SQL = SELECT
`team`,
`position`,
`votesCast`
FROM
Teamtable
$结果= $ db->查询($ 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>;
}
}
我必须尝试过点,任何帮助表示赞赏。
解决方案
这可能对您非常有帮助
$ b $
while($ row = mysqli_fetch_assoc($ result)){
$ data [$ row ['position']] ['total'] [] = $ row [ '投票'];
$ 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>。$百分比。 '%< / TD>< / TR>';
}
}
echo'< / table>';
}
I have a table that contains Aspiring team for particular positions and various vote casted. The data is below
Teamtable
| no | team | position | votes Cast |
| 1 | A | President | 2 |
| 3 | B | President | 1 |
| 4 | C | Secretary | 2 |
| 6 | D | Secretary | 1 |
I want to be able to get this in an html format using php and mysql just as below
EXPECTED VIEW IN THE HTML AND PHP FORMAT
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 |
This is what i have tried so far
//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>";
}
}
I have to tried up to this point, any help is appreciated.
解决方案
This could be very helpful for you
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>';
}
这篇关于如何使用html和php以特定方式格式化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文