[英]HTML table with static columns and dynamic rows
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. 我想做的( 但显然做不到 )是在第一列显示的运动中显示每个团队(CAS,CEIT,CASNR,CSE)的相应得分。
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>
PS If there's no data available from table score
for that team in a specific sport, it still should display zero. PS:如果在特定运动中该队的表
score
没有可用数据,则该值仍应显示为零。
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. 关于得分 :获取sportId并从得分表中获取结果,然后再次循环。 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 . 警告: 请不要在新代码中使用
mysql_*
函数 。 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.
相反,要了解准备好的语句 ,并使用PDO或MySQLi- 本文将帮助您确定哪一个。 If you choose PDO, here is a good tutorial .
如果您选择PDO, 这是一个很好的教程 。
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 如果不行,请给我表格结构
运动和得分表之间是否存在任何链接。可以使用下面的左联接。
select * from sports left join score on sports.sportsid = score.sportsid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.