0

enter image description here

Let the image above explain it briefly.

What I want to do (but obviously can't) is to display the corresponding score of each teams (CAS, CEIT, CASNR, CSE) in the sports displayed in the first column.

This is what I have so far..

<table class="table table-bordered">
        <thead>
        <tr>
          <th>Games</th>
          <th class="danger">CAS</th>
          <th class="warning">CEIT</th>
          <th class="success">CASNR</th>
          <th class="info">CSE</th>
        </tr>
        </thead>
        <tbody>
        <?php
        include('connection.php');
              $sportid = '';
              $query=mysql_query("SELECT * FROM sports ORDER BY sportname")or die(mysql_error());
              while($row=mysql_fetch_array($query))     {
                      $sportid = $row['sportid'];
            ?>
        <tr>
        <td><input type="hidden" id="sportid[]" name="sportid[]" value="<?php echo $row['sportid']; ?>"><?php echo $row['sportname']; ?> </td>
        <td>0</td>
        <td>0</td>
        <td>0</td>
        <td>0</td>      
        <?php  } ?>
        </tr>
        <tr>
          <td class="success">Total Points</td>
          <td class="info">0</td>
          <td class="info">0</td>
          <td class="info">0</td>
          <td class="info">0</td>
        </tr>
      </tbody>
</table>

P.S. If there's no data available from table score for that team in a specific sport, it still should display zero.

eirishainjel
  • 570
  • 7
  • 25

3 Answers3

1

For your first query regarding dynamic table heading:

 <table class="table table-bordered">
    <thead>
    <tr>
 <?php
    include('connection.php');
    $sportid = '';
    $query=mysql_query("SELECT * FROM sports ORDER BY sportname")or die(mysql_error());
    while($row=mysql_fetch_array($query))     {
          $sportid = $row['sportid'];
    ?>

      <th><input type="hidden" id="sportid[]" name="sportid[]" value="<?php echo $row['sportid']; ?>"><?php echo $row['sportname']; ?> </th>
   <?php } ?>
   </tr>
   </thead>

Regarding score: Take sportId and fetch result from score table and loop again. Also its better if you can use joins to get desire results. Have a try and come back again with your efforts.

Warning: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1

try this code

    <?php 
    $headers=array('CAS','CEIT','CASNR','CSE');
?>
<table class="table table-bordered">
        <thead>
        <tr>
          <th>Games</th>
          <th class="danger">CAS</th>
          <th class="warning">CEIT</th>
          <th class="success">CASNR</th>
          <th class="info">CSE</th>
        </tr>
        </thead>
        <tbody>
        <?php
        include('connection.php');
              $sportid = '';
              $query=mysql_query("SELECT * FROM sports ORDER BY sportname")or die(mysql_error());
              while($row=mysql_fetch_array($query))     {
                    $values=array();
                      $sportid = $row['sportid'];
                      for ($i = 0; $i < count($headers); $i++) {
                        $query1=mysql_query("SELECT score FROM score WHERE sportid= ".$sportid." AND team = ".$headers[$i])or die(mysql_error());
                        while ($row1 = mysql_fetch_array($query1)) {
                            $values[$i]=$row1['score'];
                        }
                      }
        ?>
        <tr>
        <td><input type="hidden" id="sportid[]" name="sportid[]" value="<?php echo $row['sportid']; ?>"><?php echo $row['sportname']; ?> </td>
        <td><?php echo $values[0]; ?></td>
        <td><?php echo $values[1]; ?></td>
        <td><?php echo $values[2]; ?></td>
        <td><?php echo $values[3]; ?></td>      
        <?php echo '</tr>';} ?>

        <tr>
          <td class="success">Total Points</td>
          <td class="info">0</td>
          <td class="info">0</td>
          <td class="info">0</td>
          <td class="info">0</td>
        </tr>
      </tbody>
</table>

if not work give me the tables structure

tom fox
  • 126
  • 1
  • 3
  • 12
0

Is there any link between sport and score table if it is there .Than you can use below left join.

select * from sports left join score on sports.sportsid = score.sportsid