Skip to content
Advertisement

How to display each row from sql in a table in php

I have a table within a modal. I want the table to display the top 3 goalscorers from a football game. The following code is not returning the table. Any help greatly appreciated!. At present I am trying to echo the table within the php tags but I am not sure if this is the best way to do this I took this approach as I am expecting three rows to be returned from the database.

<div class="modal fade bd-example-modal-lg" id="homeleaderboard" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">

  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <h3 style="text-align: center;">Most Goals</h3>


      <?php
      $topgoals="SELECT player,panel.name,count(*) AS goalcount FROM fixtures
      inner join panel
      on fixtures.player=panel.id
      where fixture='$fixture_id' and goal='1'
      group by player
      order by goalcount desc
      limit 3";

      $goalsresult=mysqli_query($conn,$topgoals) or die(mysqli_error($conn));

      if(mysqli_num_rows($result)>0){
        while($row=mysqli_fetch_assoc($result)){
          $goalnames=$row['name'];
          $goaltotal=$row['goalcount'];
          echo "<div class='table-responsive'>
                <table class='table table-striped table-bordered'>
                <thead>
                <tr>
                <th>Player</th>
                <th>Goals</th>
                </tr>
                </thead>
                <tr>
                <td>$goalnames</td>
                <th>$goaltotal</th>


                </tr>
                </table>
                </div>

                  ";
      }
        }

       ?>

  </div>
</div>
</div>
         </div>

Advertisement

Answer

Noticed you define $goalsresult to store the result set from your mysql_query call and think you’d want to pass that instead of $result – unless $result is something you have defined elsewhere and are using intentionally.

Here’s an example with your code:

$goalsresult = mysqli_query($conn,$topgoals) or die(mysqli_error($conn));

if(mysqli_num_rows($goalsresult)>0) {
     while($row = mysqli_fetch_assoc($goalsresult)){

Hope this gets you what you need.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement