1

我有一个 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>

目前只输出面板名称。任何帮助或建议都非常感谢!

4

2 回答 2

0

DISTINCT从 sql 中删除。你也想ORDER BY grade在表名之后。

要获得您显示的格式,您需要检测循环中成绩何时发生变化,然后打印标题行。

于 2013-03-16T10:35:51.287 回答
0

一个问题是您应该在要比较的值周围加上单引号:

panel = '$panel_names['panel']' " );

另一个问题是 php 不喜欢在嵌入字符串时解析带有引号的数组键。您可以将数组包围在 {} 中:

panel = '{$panel_names['panel']}' " );
于 2013-03-16T12:10:00.000 回答