我有一个MySQL表,其值包含体育比赛的评委。布局示例:
Table name = competition
panel | name | role
Grade 1 | Bob | Judge 1
Grade 1 | Dave | Judge 2
Grade 2 | Jo | Judge 1
Grade 1 | Kat | Judge 3
我想以下列格式在HTML中显示:
-- Grade 1 --
Judge 1 | Bob
Judge 2 | Dave
Judge 3 | Kat
-- Grade 2 --
Judge 1 | Jo
面板名称是可变的,并且在竞争之间发生变化,因此在代码中使用它们不是一种选择。我的尝试(不起作用)是:
<?php $panels_distinct = mysql_query("SELECT DISTINCT panel FROM competition"); ?>
<table>
<?php while $panel_names = mysql_fetch_array($panels_distinct) { ?>
<tr>
<td colspan="2"><?php echo $panel_names['panel'] ; ?></td>
</tr>
<?php $judges = mysql_query("SELECT * FROM competition WHERE panel = $panel_names['panel'] ") ;
while $judges_info=mysql_fetch_array($judges) { ?>
<tr>
<td><?php echo $judges_info['role'] ; ?></td>
<td><?php echo $judges_info['name'] ; ?></td>
</tr>
<?php } // end while $judges_info ?>
<?php } // end while $panel_names ?>
</table>
目前只输出面板名称。任何帮助或建议感激不尽!
答案 0 :(得分:0)
从sql中删除DISTINCT
。您还希望在表名后加ORDER BY grade
。
要获得您显示的格式,您需要检测循环中的成绩何时更改,然后打印标题行。
答案 1 :(得分:0)
一个问题是你应该在你要比较的值周围加上单引号:
panel = '$panel_names['panel']' " );
另一个问题是,php不喜欢在嵌入字符串时解析在它们周围引用引号的数组键。您可以在{}中包围数组:
panel = '{$panel_names['panel']}' " );