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.

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

yey! it works. but i'm getting undefined offset:0 in <td> that doesn't have available data. but thanks anyway! :D
you can check if isset the value of the array every time and if not echo 0
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.