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

时间:2015-02-12 07:02:58

标签: php mysql

enter image description here

让上面的图片简要解释一下。

我想做的事情(但显然不能)是显示第一栏中显示的各项球队(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>

P.S。如果特定运动中该团队的score表中没有可用数据,则仍应显示为零。

3 个答案:

答案 0 :(得分:1)

关于动态表标题的第一个查询:

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

关于得分:获取sportId并从得分表中获取结果并再次循环。如果你可以使用连接来获得欲望结果,它也会更好。试试并再次努力回来。

警告: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:1)

试试这段代码

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

如果不行,请给我表结构

答案 2 :(得分:0)

运动和得分表之间是否有任何关联。如果它在那里。你可以使用左下方的加入。

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