我创建了列表页面,其中包含分页和按列名称排序。分页工作正常,但按列名搜索无法按预期工作。 这是我的代码:
的index.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>
任何人都可以告诉我我错过了什么.... :)
答案 0 :(得分:0)
Hi your code is working good there is no need to bellow code remove it or comment it
if (isset($_GET['order']) && $_GET['order'] == "ASC") {
asort($result);
}
答案 1 :(得分:0)
//从代码中删除以下行
$where.=" ORDER BY " . $_GET['orderBy'] . " " . $order;
//而不是代码
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>";
}
//添加以下代码
$orderBy="articleTitle";
if (isset($_GET['orderBy'])) {
$orderBy=$_GET['orderBy'];
}
$j=0;
$content=array();
while ($row = mysql_fetch_array($result)) {
$content[$j]['articleTitle']=$row['articleTitle'];
$content[$j]['articleSubTitle']=$row['articleSubTitle'];
$j++;
}
$content=sortByOneKey($content,$orderBy,$order);
foreach ($content as $value){
echo "<tr>";
echo "<td style='text-align:center'>" . $value['articleTitle'] . "</td>";
echo "<td style='text-align:center'>" . $value['articleSubTitle'] . "</td>";
echo "</tr>";
}
//在页面中添加以下功能
<?php
//this function is for sorting the array by using key value
function sortByOneKey(array $array, $key, $asc = 'ASC') {
$result = array();
$values = array();
foreach ($array as $id => $value) {
$values[$id] = isset($value[$key]) ? $value[$key] : '';
}
if ($asc=="DESC") {
arsort($values);
}
else {
asort($values);
}
foreach ($values as $key => $value) {
$result[$key] = $array[$key];
}
return $result;
}
?>
希望这对你有用...... !!! :)