我已经创建了带有分页和按列名排序的列表页面。分页工作正常,但按列名搜索未按预期工作。这是我的代码:
索引.php
<table>
<tr>
<?php
$page = "";
if (isset($_GET['page'])) {
$page = "&page=" . $_GET['page'] . "";
}
$order = "&order=ASC";
if (isset($_GET['order']) && $_GET['order'] == "ASC") {
$order = "&order=DESC";
}
?>
//用于排序记录的带有链接的列名
<th><a href='index.php?<?php echo $order ?>&orderBy=articleTitle<?php echo $page ?>' >Article Title</a></th>
<th><a href='index.php?<?php echo $order ?>&orderBy=articleSubTitle<?php echo $page ?>' >Article SubTitle</a></th>
</tr>
<?php
$perpage = 2;
if (isset($_GET["page"])) {
$page = intval($_GET["page"]);
} else {
$page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;
$where = "";
if (isset($_GET['orderBy'])) {
$order = "ASC";
if (isset($_GET['order']) && $_GET['order'] == "ASC") {
$order = "DESC";
}
$where.=" ORDER BY " . $_GET['orderBy'] . " " . $order;
}
$result = mysql_query("SELECT * FROM xp_article $where Limit $start, $perpage");
$i = 0;
if (isset($_GET['order']) && $_GET['order'] == "ASC") {
asort($result);
}
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td style='text-align:center'>" . $row['articleTitle'] . "</td>";
echo "<td style='text-align:center'>" . $row['articleSubTitle'] . "</td>";
echo "</tr>";
}
?>
//分页码
<tr>
<th colspan="3" class="paging" style="text-align: center">
<?php
if (isset($page)) {
$result1 = mysql_query("SELECT Count(*) As Total FROM xp_article");
$rows = mysql_num_rows($result1);
if ($rows) {
$rs = mysql_fetch_array($result1);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if ($page <= 1) {
echo "<span>Prev</span>";
} else {
$j = $page - 1;
echo "<span><a href='index.php?page=$j'>< Prev</a></span>";
}
for ($i = 1; $i <= $totalPages; $i++) {
if ($i <> $page) {
echo " <span><a href='index.php?page=$i' >$i</a></span>";
} else {
echo " <span>$i</span>";
}
}
if ($page == $totalPages) {
echo " <span>Next ></span>";
} else {
$j = $page + 1;
echo " <span><a href='index.php?page=$j'>Next</a></span>";
}
}
?>
</th>
</tr>
</table>
谁能告诉我我错过了什么...... :)