如何将其制作成表并按字母顺序对其进行排序

时间:2015-01-29 15:50:14

标签: php

这是我的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";
}

1 个答案:

答案 0 :(得分:0)

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

要执行此操作,您需要执行常规查询并在其末尾添加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";
  }
?>
相关问题