简体   繁体   English

PHP PDO MySQL填充HTML表

[英]PHP PDO MySQL Populate HTML Table

I've searched and searched and I'm still pretty lost. 我已经搜寻了,但我仍然很迷路。 What I struggle with is some basics, but I'm usually pretty good at figure things out from examples. 我所苦的是一些基础知识,但是我通常很擅长从示例中弄清楚事情。

What I need help with: I can't seem to populate a Golf Scorecard HTML table based on my MySQL query. 我需要帮助的地方:我似乎无法基于MySQL查询填充Golf Scorecard HTML表。 For whatever reason, I wish I knew, the last golfer in my table gets all the values. 无论出于何种原因,我希望我知道,表中的最后一位高尔夫球手都能获得所有值。 I can't figure our a way for the data to loop correctly. 我无法弄清楚数据正确循环的方法。 At hole 9, the loop should stop and move to the next row for the next golfer. 在第9洞,套环应停止并移至下一位高尔夫球手的下一行。

I've been trying Whiles and Foreach loops to figure out how exactly they work which is why I have both. 我一直在尝试Whiles和Foreach循环来弄清楚它们的工作原理,这就是为什么我同时拥有这两个原因的原因。 I plan on combining the two queries into one, once I figure out the individual outputs. 一旦弄清单个输出,我计划将两个查询合并为一个。

I really appreciate any guides. 我真的很感谢任何指导。 I've been searching for a solution for a few weeks now, and I'm realizing I'm quite stuck. 我一直在寻找解决方案已有几个星期了,但我意识到自己已经陷入困境。

Current Golf Score Output 当前高尔夫成绩输出


PHP PDO Queries 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);

Array Output For $Scores $ 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
    )

Bootstrap Table 引导表

<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 Query. 获取回合分数查询。 The query below allows me to get pretty much everything I needed in 1 query. 下面的查询使我几乎可以在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);

Then I had some help from a guy at work to help me understand how to create my own arrays to help with the data. 然后,我得到了一个工作人员的帮助,以帮助我了解如何创建自己的数组来帮助数据。

<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>

Definitely not the best way to get the job done, but it did get the job done. 绝对不是完成工作的最佳方法,但确实可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM