我有一个由MySQL数据库中的数据填充的html表。我有这个表的两个版本,第一个显示一个表中的所有数据,而另一个显示分页格式。我创建了第一个表,以便当您单击其中一个表头时,它会按升序或降序交替显示。以下是该表的代码:
$sql = "SELECT id, filepath, filename, type FROM files ";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'filepath') {
$query = $sql . "ORDER BY filepath ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'filepathdesc') {
$query = $sql . "ORDER BY filepath DESC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'filename') {
$query = $sql . "ORDER BY filename ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'filenamedesc') {
$query = $sql . "ORDER BY filename DESC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'type') {
$query = $sql . "ORDER BY type ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'typedesc') {
$query = $sql . "ORDER BY type DESC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'id') {
$query = $sql . "ORDER BY id ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'iddesc') {
$query = $sql . "ORDER BY id DESC";
} else {
$query = "SELECT id, filepath, filename, type FROM files ORDER BY id";
}
if ($result = $db->query($query, MYSQLI_STORE_RESULT)) {
// Display records if they are in the db
if ($result->num_rows > 0) {
// Open table tag
echo "<table border='1' cellpadding='10'>";
// Set table headers
echo "<tr> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'iddesc') {
echo "<a href = '?orderby=id'>ID</a>";
} else {
echo "<a href = '?orderby=iddesc'>ID</a>";
}
echo "</th> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'filepath') {
echo "<a href = '?orderby=filepathdesc'>Filepath</a>";
} else {
echo "<a href = '?orderby=filepath'>Filepath</a>";
}
echo "</th> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'filename') {
echo "<a href = '?orderby=filenamedesc'>Filename</a>";
} else {
echo "<a href = '?orderby=filename'>Filename</a>";
}
echo "</th> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'type') {
echo "<a href = '?orderby=typedesc'>Type</a>";
} else {
echo "<a href = '?orderby=type'>Type</a>";
}
echo "</th> "
. "<th></th> "
. "</tr>";
// Loop through the results of the database query and display them in the table
while ($row = $result->fetch_object()) {
// Set a row for each record
echo "<tr>";
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->filepath . '</td>';
echo '<td>' . $row->filename . '</td>';
echo '<td>' . $row->type . '</td>';
echo '<td><a href="deletefile.php?id=' . $row->id . '" onclick=\'javascript:return confirm("Are you sure you want to delete this file?"); \'>Delete</a></td>';
echo "</tr>";
}
echo "</table>";
}
// Let user know if there are no records in the database
else {
echo "<p><b>No results to display!</b></p>";
}
但是,我尝试在过去四个小时内使用类似代码对分页版本执行相同操作但未成功。以下是分页版本的代码:
$sql = "SELECT id, filepath, filename, type FROM files ";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'filepath') {
$query = $sql . "ORDER BY filepath ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'filepathdesc') {
$query = $sql . "ORDER BY filepath DESC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'filename') {
$query = $sql . "ORDER BY filename ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'filenamedesc') {
$query = $sql . "ORDER BY filename DESC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'type') {
$query = $sql . "ORDER BY type ASC";
} else if (isset($_GET['orderby']) && $_GET['orderby'] == 'typedesc') {
$query = $sql . "ORDER BY type DESC";
} else if (isset($_GET['orderby']) && isset($_GET['page']) && $_GET['orderby'] == 'id') {
$query = $sql . "ORDER BY id ASC";
} else if (isset($_GET['orderby']) && isset($_GET['page']) && $_GET['orderby'] == 'iddesc') {
$query = $sql . "ORDER BY id DESC";
} else {
$query = "SELECT id, filepath, filename, type FROM files ORDER BY id";
}
if ($result = $db->query($query, MYSQLI_STORE_RESULT)) {
if ($result->num_rows != 0) {
$totalResults = $result->num_rows;
// ceil() returns the next highest integer value by rounding up if necessary
$total = ceil($totalResults / $pageSize);
// Check id the page variable is set in the URL (ex: pagedView.php?page=1
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$showPage = $_GET['page'];
// Make sure that $showPage value is valid
if ($showPage > 0 && $showPage <= $total) {
$start = ($showPage - 1) * $pageSize;
$end = $start + $pageSize;
} else {
// Error: show first set
$start = 0;
$end = $pageSize;
}
} else {
// If page isn't set, show first set
$start = 0;
$end = $pageSize;
}
// Display navigation
echo "<p><a href='displayfiles.php'>View all</a> | <b>View page:</b> ";
for ($i = 1; $i <= $total; $i++) {
if (isset($_GET['page']) && $_GET['page'] == $i) {
echo $i . " ";
} else {
echo "<a href='displaypagedfiles.php?page=$i'>$i</a> ";
}
}
echo "</p>";
echo "<p><a href='fileupload.php'>Upload a new file</a></p>";
// Display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'iddesc') {
echo "<a href = '?page=$i&orderby=id'>ID</a>";
} else {
echo "<a href = '?page=$i&orderby=iddesc'>ID</a>";
}
echo "</th> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'filepath') {
echo "<a href = '?page=$i&orderby=filepathdesc'>Filepath</a>";
} else {
echo "<a href = '?page=$i&orderby=filepath'>Filepath</a>";
}
echo "</th> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'filename') {
echo "<a href = '?page=$i&orderby=filenamedesc'>Filename</a>";
} else {
echo "<a href = '?page=$i&orderby=filename'>Filename</a>";
}
echo "</th> "
. "<th>";
if (isset($_GET['orderby']) && $_GET['orderby'] == 'type') {
echo "<a href = '?page=$i&orderby=typedesc'>Type</a>";
} else {
echo "<a href = '?page=$i&orderby=type'>Type</a>";
}
echo "</th> "
. "<th></th> "
. "</tr>";
// Loop through query results and display in tbale
for ($i = $start; $i < $end; $i++) {
if ($i == $totalResults) {
break;
}
// go to specific row
$result->data_seek($i);
$row = $result->fetch_row();
// echo contents of each row in table
echo "<tr>";
echo '<td>' . $row[0] . '</td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td><a href="deletefile.php?id=' . $row[0] . '" onclick=\'javascript:return confirm("Are you sure you want to delete this file?"); \'>Delete</a></td>';
echo "</tr>";
}
echo "</table>";
} else {
echo "No results to show!";
}
} else {
// If query error
echo "Error: " . $db->error;
}
任何帮助都会非常感激,因为我已经坚持这么久了!另外,我对PHP很新,所以如果我的代码有点乱,或者你发现我的代码有任何其他问题,请告诉我。
答案 0 :(得分:0)
由于我正确理解您的代码,您正在尝试使用PHP而不是MySQL进行分页。 MySQL具有向服务器发送LIMIT
OFFSET
的简洁扩展。
这可能是这样的:
SELECT id, filepath, filename, type
FROM files
ORDER BY $your_sort_option
LIMIT $offset, $per_page
所以基本上,你需要两个查询。第一个检索总数的元素来创建分页器,第二个来获取数据。
当你得到$_GET['page']
时,你可以像这样计算两个变量:
$page = (int) $_GET['page'];
$per_page = 10;
$offset = ($page - 1) * $per_page;
然而,正如我在此解释的那样,使用LIMIT
可能会非常缓慢:http://www.xarg.org/2011/10/optimized-pagination-using-mysql/
迎接