【问题标题】:Filter SQL Data on HTML Table过滤 HTML 表上的 SQL 数据
【发布时间】:2014-04-23 00:17:41
【问题描述】:

我正在尝试找到一种方法来从我的 html 表上的 mysql 数据库中过滤我的 sql 数据。我遇到的麻烦是因为它是一个动态表。我是否应该对 html 表进行排序,如果是,我如何从 sql 查询(整个表)中获取结果 html,而不是仅单行的 html。

如果我需要对 SQL 查询本身进行排序,我怎样才能做到这一点而不需要每次都刷新页面?

感谢您的帮助!

这是我的参考代码:

<?php

$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
}
$result=mysql_query($sql);
?>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
</head>
<table id="forum" width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<thead>
<tr>
<th width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<th width="43%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<th width="10%" align="center" bgcolor="#E6E6E6"><strong>Author</strong></td>
<th width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<th width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<th width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
</thead>
<?php 

// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tbody>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['threadtype']; ?></td>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
</tbody
<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tfoot>
<tr>
<td colspan="6" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>
</tr>
</tfoot>
</table>
</html>

【问题讨论】:

    标签: javascript php jquery mysql ajax


    【解决方案1】:

    如果您没有大量数据 [少于 100 行],使用一个查询加载整个数据并在客户端执行所有排序/过滤是合理的。

    有很多很棒的 jquery 插件可以做到这一点。这是一个我喜欢的好:

    https://datatables.net/

    如果您想要可扩展并处理大量行,那么您不想一次加载所有行。

    在这种情况下,您希望使用查询实现分页模式。如果您需要进行分页,那么当用户更改他们想要排序的列时,您还需要在后端对查询进行排序。

    与其重新发明轮子,尤其是如果您不喜欢在这里自己动手,还可以使用很多工具来做到这一点。这个看起来很有希望:http://blog.drale.com/mysql-table-viewer-with-pagination-and-sorting/

    【讨论】:

    • 感谢您的帮助!我已经查看了 Datatables 插件,但我打算有一个大表。您发布的第二个链接看起来与我正在寻找的完全一样。
    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多