具有静态列和动态行的HTML表

具有静态列和动态行的HTML表

让上图简要说明它。

我想做的(但显然做不到)是在第一栏中显示的运动中显示每支球队(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))     {
                      $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>


附言如果表score中没有该特定运动队的可用数据,则该值仍应显示为零。

最佳答案

试试这个代码

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


如果不行,请给我表格结构

关于php - 具有静态列和动态行的HTML表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28471280/

10-09 14:03