0

我有一个表从单个查询回显到我们数据库中的一个表,我让它回显下表;

http://www.skulldogs.com/dev/testview.php

我希望它在“mainToon”名称匹配的正确绿色行下对“黄色”行进行排序,例如:

高压

--真正的麦考伊

--cpt钩子

里亚萨尔

-- 淡水河谷

我回显上述页面的代码是;

<?php

  $result = mysql_query("SELECT * FROM `members`");
  echo "<table border='1'>
  <tr>
  <th>Character ID</th>
  <th>Name</th>
  <th>MainToon</th>
  <th>toonCategory</th>
  </tr>";
    while ($row = mysql_fetch_array($result)) {
        $characterID = $row['characterID'];
        $name = $row['name'];
        $startDateTime = $row['startDateTime'];
        $logonDateTime = $row['logonDateTime'];
        $logoffDateTime = $row['logoffDateTime'];
        $location = $row['location'];
        $role = $row['role'];
        $vouchedBy = $row['vouchedBy'];
        $positionHeld = $row['positionHeld'];
        $remarks = $row['remarks'];
        $afkNotice = $row['afkNotice'];
        $toonCategory = $row['toonCategory'];
        $mainToon = $row['mainToon'];
        $watch = $row['watch'];

    if ($toonCategory == 'Main Toon') {
        echo "<tr bgcolor='#00FF00'>"; }
    else {
        echo "<tr bgcolor='#FFFF00'>"; }
    echo "<td>" . $characterID . "</td>";
    echo "<td>" . $name . "</td>";
    echo "<td>" . $mainToon . "</td>";
    echo "<td>" . $toonCategory . "</td>";
    echo "</tr>";
  }
    echo "</table>";

?>

目前我不会回显其他数据,直到我能弄清楚如何相应地显示此表。可以这样做吗?

这就是我想要显示表格的方式;

http://www.skulldogs.com/dev/mockup.php

4

2 回答 2

4

在你的 sql 中添加一个ORDER BY子句:

SELECT * FROM `members` ORDER BY toonCategory;

如果“Main Toon”上下还有其他值,您可以通过布尔值排序:

SELECT * FROM `members` ORDER BY toonCategory = 'Main Toon' DESC;

编辑:

现在,我看到了您提出示例后的要求,请尝试:

SELECT * FROM `members` ORDER BY CONCAT(MainToon, Name);

如果空格是空字符串或:

SELECT * FROM `members` ORDER BY COALESCE(MainToon, Name) DESC, Name;

如果空白行为空。

于 2013-02-25T23:37:16.090 回答
1

尝试 SELECT * FROM membersORDER BY toonCategory;

于 2013-02-25T23:38:33.537 回答