PHP PDO MySQL填充HTML表
我找了又找,但还是很迷路。我所挣扎的是一些基础知识,但我通常很擅长从例子中找出问题 我需要的帮助:我似乎无法基于MySQL查询填充高尔夫记分卡HTML表。不管出于什么原因,我希望我知道,我桌上的最后一个高尔夫球手得到了所有的价值。我无法找到数据正确循环的方法。在第9洞,环路应停止并移动到下一排,以便下一位高尔夫球手使用 我一直在尝试Whiles和Foreach循环,以弄清楚它们到底是如何工作的,这就是为什么我有这两个循环。我计划在计算出各个输出后,将两个查询合并为一个查询 我真的很感激任何导游。几周来我一直在寻找解决方案,现在我意识到我被卡住了PHP PDO MySQL填充HTML表,php,mysql,pdo,bootstrap-table,Php,Mysql,Pdo,Bootstrap Table,我找了又找,但还是很迷路。我所挣扎的是一些基础知识,但我通常很擅长从例子中找出问题 我需要的帮助:我似乎无法基于MySQL查询填充高尔夫记分卡HTML表。不管出于什么原因,我希望我知道,我桌上的最后一个高尔夫球手得到了所有的价值。我无法找到数据正确循环的方法。在第9洞,环路应停止并移动到下一排,以便下一位高尔夫球手使用 我一直在尝试Whiles和Foreach循环,以弄清楚它们到底是如何工作的,这就是为什么我有这两个循环。我计划在计算出各个输出后,将两个查询合并为一个查询 我真的很感激任何导游。
PHP PDO查询
$Scores的数组输出
引导表
洞
标准杆数
全部的
获取四舍五入分数查询。下面的查询允许我在1个查询中获得几乎所有我需要的东西
//Get Round Scores
$scoresSQL = $auth_user->runQuery("SELECT tblUsers.user_first, tblUsers.user_last, tblScore.PlayerID, tblScore.RoundID, tblScore.HoleNum, tblScore.NumStrokes, tblScore.NumPutts, tblScore.FIR, tblScore.GIR FROM tblScore INNER JOIN tblUsers ON tblScore.PlayerID=tblUsers.user_id WHERE RoundID=:round_id ORDER BY tblScore.PlayerID, tblScore.HoleNum ASC");
$scoresSQL -> execute(array(":round_id"=>$roundID));
$scores = $scoresSQL -> fetchALL(PDO::FETCH_ASSOC);
然后,我得到了一位同事的帮助,帮助我了解如何创建自己的阵列来帮助处理数据
<table class="table table-bordered m-b-0" id="roundupload">
<thead>
<tr>
<th colspan="1">Hole</th>
<?php
while ($HoleNum = $holeSQL->fetch(PDO::FETCH_ASSOC)){
echo'<th>'.$HoleNum['HoleNum'].'</th>'
;}
?>
</tr>
<tr>
<th colspan="1">Par</th>
<?php
while ($parInfo = $parSQL->fetch(PDO::FETCH_ASSOC)){
echo'<th><span class="label label-success">'.$parInfo['Par'].'</span></th>'
;}
?>
<th class="text-muted">Total</th>
</tr>
</thead>
<tbody>
<!-- Pull in Shooter Names For Upload Table -->
<?php
$players = array();
echo '<tr>';
foreach ($scores as $result){
if (!$players[$result['PlayerID']]['totalStrokes'] ) {
$players[$result['PlayerID']]['totalStrokes'] = null;
}
if (!$players[$result['PlayerID']]['totalPutts'] ) {
$players[$result['PlayerID']]['totalPutts'] = null;
}
$players[ $result[ 'PlayerID' ] ][ 'Name' ] = array('first' => $result[ 'user_first' ],'last' => $result[ 'user_last' ]);
$players[ $result[ 'PlayerID' ] ][ 'Hole' ][ ] = array('Hole' => $result[ 'HoleNum' ],'Strokes' => $result[ 'NumStrokes' ],'Putts' => $result[ 'NumPutts' ]);
$players[ $result[ 'PlayerID' ] ][ 'totalStrokes' ] += ( $result[ 'NumStrokes' ] );
$players[ $result[ 'PlayerID' ] ][ 'totalPutts' ] += ( $result[ 'NumPutts' ] );
}
foreach($players as $player){
echo '<th class="text-muted" rowspan="1">'.$player['Name']['first'].' '.$player['Name']['last'].'</th>';
echo '<td>'.$player['Hole']['0']['Strokes'].' | '.$player['Hole']['0']['Putts'].'</td>';
echo '<td>'.$player['Hole']['1']['Strokes'].' | '.$player['Hole']['1']['Putts'].'</td>';
echo '<td>'.$player['Hole']['2']['Strokes'].' | '.$player['Hole']['2']['Putts'].'</td>';
echo '<td>'.$player['Hole']['3']['Strokes'].' | '.$player['Hole']['3']['Putts'].'</td>';
echo '<td>'.$player['Hole']['4']['Strokes'].' | '.$player['Hole']['4']['Putts'].'</td>';
echo '<td>'.$player['Hole']['5']['Strokes'].' | '.$player['Hole']['5']['Putts'].'</td>';
echo '<td>'.$player['Hole']['6']['Strokes'].' | '.$player['Hole']['6']['Putts'].'</td>';
echo '<td>'.$player['Hole']['7']['Strokes'].' | '.$player['Hole']['7']['Putts'].'</td>';
echo '<td>'.$player['Hole']['8']['Strokes'].' | '.$player['Hole']['8']['Putts'].'</td>';
echo '<td>'.$player['totalStrokes'].' | '.$player['totalPutts'].'</td></tr>';
}
?>
</tbody>
</table>
洞
标准杆数
全部的
绝对不是完成工作的最佳方式,但它确实完成了工作 您在哪里设置
$scores
?这里没有设置$scores
到foreach($arr as$userInfo)
中特定玩家的分数。所以每次循环都是一样的,谢谢你的提醒。我已经添加了对$scores的查询。我还删除了我原来的2个数组,因为它们与我的问题无关。我添加了查询$scores时得到的数组的一部分@Barmar为什么$scoresSQL
需要加入tblUsers
?它从不使用那张桌子上的任何东西。为什么它在不使用任何聚合函数的情况下使用groupby
?我看不出为什么$scores
只包含playerID=2
的分数。查询中没有任何内容将其限制为特定玩家。
<div class="table-responsive">
<!--<form class="tr" method="post" action="roundupload.php">-->
<table class="table table-bordered m-b-0" id="roundupload">
<thead>
<tr>
<th colspan="1">Hole</th>
<?php
while ($HoleNum = $holeSQL->fetch(PDO::FETCH_ASSOC)){
echo'<th>'.$HoleNum['HoleNum'].'</th>'
;}?>
</tr>
<tr>
<th colspan="1">Par</th>
<?php
while ($parInfo = $parSQL->fetch(PDO::FETCH_ASSOC)){
echo'<th><span class="label label-success">'.$parInfo['Par'].'</span></th>'
;}?>
<th class="text-muted">Total</th>
</tr>
</thead>
<tbody>
<!-- Pull in Shooter Names For Upload Table -->
<?php
foreach($arr as $userInfo){
?>
<tr class="tableRow">
<td class="text-muted" rowspan="1">
<?php echo
$userInfo['user_first'].' '.$userInfo['user_last']?>
</td>
<?php
;}
?>
<?php
foreach ($scores as $holescore){
echo'<td>'.$holescore['NumStrokes'].'</td>'
;}?>
<td id="hole4:h4" class="inner strokes" contenteditable="true"></td>
<td id="stroketotal:s1" class="inner-total"></td>
</tr>
</tbody>
</table>
<!--</form>-->
</div>
//Get Round Scores
$scoresSQL = $auth_user->runQuery("SELECT tblUsers.user_first, tblUsers.user_last, tblScore.PlayerID, tblScore.RoundID, tblScore.HoleNum, tblScore.NumStrokes, tblScore.NumPutts, tblScore.FIR, tblScore.GIR FROM tblScore INNER JOIN tblUsers ON tblScore.PlayerID=tblUsers.user_id WHERE RoundID=:round_id ORDER BY tblScore.PlayerID, tblScore.HoleNum ASC");
$scoresSQL -> execute(array(":round_id"=>$roundID));
$scores = $scoresSQL -> fetchALL(PDO::FETCH_ASSOC);
<table class="table table-bordered m-b-0" id="roundupload">
<thead>
<tr>
<th colspan="1">Hole</th>
<?php
while ($HoleNum = $holeSQL->fetch(PDO::FETCH_ASSOC)){
echo'<th>'.$HoleNum['HoleNum'].'</th>'
;}
?>
</tr>
<tr>
<th colspan="1">Par</th>
<?php
while ($parInfo = $parSQL->fetch(PDO::FETCH_ASSOC)){
echo'<th><span class="label label-success">'.$parInfo['Par'].'</span></th>'
;}
?>
<th class="text-muted">Total</th>
</tr>
</thead>
<tbody>
<!-- Pull in Shooter Names For Upload Table -->
<?php
$players = array();
echo '<tr>';
foreach ($scores as $result){
if (!$players[$result['PlayerID']]['totalStrokes'] ) {
$players[$result['PlayerID']]['totalStrokes'] = null;
}
if (!$players[$result['PlayerID']]['totalPutts'] ) {
$players[$result['PlayerID']]['totalPutts'] = null;
}
$players[ $result[ 'PlayerID' ] ][ 'Name' ] = array('first' => $result[ 'user_first' ],'last' => $result[ 'user_last' ]);
$players[ $result[ 'PlayerID' ] ][ 'Hole' ][ ] = array('Hole' => $result[ 'HoleNum' ],'Strokes' => $result[ 'NumStrokes' ],'Putts' => $result[ 'NumPutts' ]);
$players[ $result[ 'PlayerID' ] ][ 'totalStrokes' ] += ( $result[ 'NumStrokes' ] );
$players[ $result[ 'PlayerID' ] ][ 'totalPutts' ] += ( $result[ 'NumPutts' ] );
}
foreach($players as $player){
echo '<th class="text-muted" rowspan="1">'.$player['Name']['first'].' '.$player['Name']['last'].'</th>';
echo '<td>'.$player['Hole']['0']['Strokes'].' | '.$player['Hole']['0']['Putts'].'</td>';
echo '<td>'.$player['Hole']['1']['Strokes'].' | '.$player['Hole']['1']['Putts'].'</td>';
echo '<td>'.$player['Hole']['2']['Strokes'].' | '.$player['Hole']['2']['Putts'].'</td>';
echo '<td>'.$player['Hole']['3']['Strokes'].' | '.$player['Hole']['3']['Putts'].'</td>';
echo '<td>'.$player['Hole']['4']['Strokes'].' | '.$player['Hole']['4']['Putts'].'</td>';
echo '<td>'.$player['Hole']['5']['Strokes'].' | '.$player['Hole']['5']['Putts'].'</td>';
echo '<td>'.$player['Hole']['6']['Strokes'].' | '.$player['Hole']['6']['Putts'].'</td>';
echo '<td>'.$player['Hole']['7']['Strokes'].' | '.$player['Hole']['7']['Putts'].'</td>';
echo '<td>'.$player['Hole']['8']['Strokes'].' | '.$player['Hole']['8']['Putts'].'</td>';
echo '<td>'.$player['totalStrokes'].' | '.$player['totalPutts'].'</td></tr>';
}
?>
</tbody>
</table>