【问题标题】:PHP PDO MySQL Populate HTML TablePHP PDO MySQL 填充 HTML 表
【发布时间】:2017-05-02 01:18:37
【问题描述】:

我找了又找,还是很迷茫。我挣扎的是一些基础知识,但我通常很擅长从例子中找出问题。

我需要帮助:我似乎无法根据我的 MySQL 查询填充高尔夫记分卡 HTML 表。不管出于什么原因,我希望我知道,我桌子上的最后一个高尔夫球手得到了所有的价值。我想不出我们的数据正确循环的方法。在第 9 洞,循环应该停止并移动到下一个高尔夫球手的下一行。

我一直在尝试 Whiles 和 Foreach 循环来弄清楚它们是如何工作的,这就是我同时拥有这两个循环的原因。一旦我弄清楚了单独的输出,我计划将这两个查询合并为一个。

我非常感谢任何指南。几周以来,我一直在寻找解决方案,但我意识到自己陷入了困境。

Current Golf Score Output


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


【解决方案1】:

获取回合分数查询。下面的查询让我可以在 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>

绝对不是完成工作的最佳方式,但它确实完成了工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-11
    • 2018-03-12
    • 2012-05-18
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    相关资源
    最近更新 更多