2015-02-12 195 views
0

enter image description hereHTML表格與靜態列和動態行

讓像上述簡單介紹一下它。

我想要做什麼(但顯然不能)是顯示在第一列中顯示的運動中的每個球隊(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的數據,它仍然應該顯示零。

回答

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> 

如果沒有工作給我的表結構

+0

yey!有用。但是我在​​中得到'undefined offset:0',它沒有可用的數據。不過謝謝! :D – eirishainjel 2015-02-12 08:13:29

+0

你可以檢查每次是否設置數組的值,如果不回顯0 – 2015-02-12 09:16:54

+0

是的!現在好了:)謝謝。 – eirishainjel 2015-02-13 00:01:56

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

0

運動和得分表之間是否有任何聯繫?如果有,那麼您可以在左下方加入。

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

是的,但我也有讓哪個隊(頭)的分數屬於.. – eirishainjel 2015-02-12 08:16:26