【发布时间】:2017-05-02 01:18:37
【问题描述】:
我找了又找,还是很迷茫。我挣扎的是一些基础知识,但我通常很擅长从例子中找出问题。
我需要帮助:我似乎无法根据我的 MySQL 查询填充高尔夫记分卡 HTML 表。不管出于什么原因,我希望我知道,我桌子上的最后一个高尔夫球手得到了所有的价值。我想不出我们的数据正确循环的方法。在第 9 洞,循环应该停止并移动到下一个高尔夫球手的下一行。
我一直在尝试 Whiles 和 Foreach 循环来弄清楚它们是如何工作的,这就是我同时拥有这两个循环的原因。一旦我弄清楚了单独的输出,我计划将这两个查询合并为一个。
我非常感谢任何指南。几周以来,我一直在寻找解决方案,但我意识到自己陷入了困境。
PHP PDO 查询
//Get Hole Info
$holeSQL = $auth_user->runQuery("SELECT HoleNum, FrontBack FROM `tblHole`
WHERE CourseID=:course_id AND FrontBack=:front_back ORDER BY
`tblHole`.`HoleNum` ASC");
$holeSQL ->
execute(array(":course_id"=>$courseID,":front_back"=>$frontback));
//Get Par Info
$parSQL = $auth_user->runQuery("SELECT Par FROM `tblHole` WHERE
CourseID=:course_id AND FrontBack=:front_back ORDER BY `tblHole`.`HoleNum`
ASC");
$parSQL ->
execute(array(":course_id"=>$courseID,":front_back"=>$frontback));
//Get Round Scores
$scoresSQL = $auth_user->runQuery("SELECT tblScore.PlayerID, tblScore.RoundID, tblScore.HoleNum, tblScore.NumStrokes, tblScore.NumPutts, tblScore.FIR, tblScore.GIR FROM tblScore WHERE RoundID=53 ORDER BY tblScore.PlayerID ASC");
$scoresSQL -> execute(array(":round_id"=>$roundID));
$scores = $scoresSQL -> fetchALL(PDO::FETCH_ASSOC);
$Scores 的数组输出
Array
(
[0] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 1
[NumStrokes] => 5
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[1] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 2
[NumStrokes] => 6
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[2] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 3
[NumStrokes] => 4
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[3] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 4
[NumStrokes] => 5
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[4] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 5
[NumStrokes] => 3
[NumPutts] => 1
[FIR] => 0
[GIR] => 0
)
[5] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 6
[NumStrokes] => 6
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[6] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 7
[NumStrokes] => 7
[NumPutts] => 3
[FIR] => 0
[GIR] => 0
)
[7] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 9
[NumStrokes] => 6
[NumPutts] => 3
[FIR] => 0
[GIR] => 0
)
[8] => Array
(
[PlayerID] => 2
[RoundID] => 53
[HoleNum] => 8
[NumStrokes] => 2
[NumPutts] => 1
[FIR] => 0
[GIR] => 1
)
[9] => Array
(
[PlayerID] => 11
[RoundID] => 53
[HoleNum] => 2
[NumStrokes] => 5
[NumPutts] => 2
[FIR] => 1
[GIR] => 0
)
[10] => Array
(
[PlayerID] => 11
[RoundID] => 53
[HoleNum] => 1
[NumStrokes] => 5
[NumPutts] => 3
[FIR] => 1
[GIR] => 0
)
[11] => Array
(
[PlayerID] => 11
[RoundID] => 53
[HoleNum] => 8
[NumStrokes] => 4
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[12] => Array
(
[PlayerID] => 11
[RoundID] => 53
[HoleNum] => 7
[NumStrokes] => 6
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
[13] => Array
(
[PlayerID] => 11
[RoundID] => 53
[HoleNum] => 6
[NumStrokes] => 6
[NumPutts] => 2
[FIR] => 0
[GIR] => 0
)
引导表
<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>
【问题讨论】:
-
你在哪里设置
$scores? -
这里没有将
$scores设置为foreach ($arr as $userInfo)中特定玩家的分数。所以每次循环都是一样的。 -
感谢您的来电。我添加了对 $scores 的查询。我还删除了我原来的 2 数组,因为它们与我的问题无关。我添加了查询 $scores 时得到的数组的一部分。 @Barmar
-
为什么
$scoresSQL需要加入tblUsers?它从不使用该表中的任何内容。以及为什么它不使用任何聚合函数时使用GROUP BY? -
我看不出为什么
$scores只包含playerID = 2的分数。查询中没有任何内容将其限制为特定玩家。
标签: php mysql pdo bootstrap-table