【发布时间】:2013-10-24 19:28:43
【问题描述】:
我正在做一个 sch 项目,我需要帮助,通过单击标题行按字母顺序对我的数据表进行排序。我使用 MySQL,我打算使用 php 来实现它。谁能帮我?谢谢。
这是我的代码:
这是一个 php 文件。
<?php
@ $db = new mysqli('localhost', 'f33s01', 'f33s01', 'f33s01');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select * from gyms";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<table border=\"3\" cellpadding=\"5\" class=\"dirtable\" style=\"margin-bottom:15px\">
<tr align=\"center\" class=\"gold\">
<th>Number</th>
<th>Gym Name</th>
<th>Address</th>
<th>Location</th>
<th width=\"18%\">Operating Hours</th>
<th>Reservation</th>
</tr>";
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<tr>";
echo "<td align=\"center\">".($i+1)."</td>";
echo "<td align=\"center\">".stripslashes($row['gymname'])."</td>";
echo "<td>".stripslashes($row['address'])."</td>";
echo "<td>".stripslashes($row['location'])."</td>";
echo "<td align=\"center\">".stripslashes($row['operatinghours'])."</td>";
echo "<td align=\"center\"><input type= \"submit\" value=\"BOOK NOW\" class=\"bookbutton\"></td>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
$result->free();
$db->close();
?>
【问题讨论】:
-
您可以为此目的使用 jquery 数据表插件。
-
你为什么有
stripslashes?数据是否在表内转义?您可以使用htmlspecialchars之类的东西来逃避来自数据库的潜在 HTML 代码。