簡體   English   中英

PHP PDO MySQL填充HTML表

[英]PHP PDO MySQL Populate HTML Table

我已經搜尋了,但我仍然很迷路。 我所苦的是一些基礎知識,但是我通常很擅長從示例中弄清楚事情。

我需要幫助的地方:我似乎無法基於MySQL查詢填充Golf Scorecard 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>

獲取回合分數查詢。 下面的查詢使我幾乎可以在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>

絕對不是完成工作的最佳方法,但確實可以完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM