如何将其制作成表格并按字母顺序排序


How do I make this into a table and sort it alphabeticaly

这是我的 php 脚本的一部分,其中数据库镜像到网页中。

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "" . $row["id"]. "" . $row["student"]. "" . $row["subject"]."".    $row["grade"]. "<br></div>";
  }
} else {
  echo "0 results";
}

因此,按字母顺序排序,它需要返回到您的查询。

为此,您需要执行常规查询并在末尾添加ORDER BY whatever_field_you_want_to_set_as_alphabetical ASC。例:

SELECT * FROM table ORDER BY name ASC

这就解决了按字母顺序排列的问题。

要将其放入表中,您需要首先检查是否有结果。

如果有,请创建表并确保在表中为每一行创建一个新行。

<?php if ($result->num_rows > 0) {
<table>
  <thead>
    <th>
      <td>ID</td>
      <td>Student</td>
      <td>Subject</td>
    </th>
  </thead>
  <tbody>
    <?php
      while($row = $result->fetch_assoc()) {
    ?>
        <tr>
          <td><?php echo $row['id'];?></td>
          <td><?php echo $row['student'];?></td>
          <td><?php echo $row['subject'];?></td>
        </tr>
    <?php
      }
    ?>
  </tbody>
</table>
<?php 
  } else {
    echo "0 Results";
  }
?>