1

我已经创建了带有分页和按列名排序的列表页面。分页工作正常,但按列名搜索未按预期工作。这是我的代码:

索引.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 "&nbsp;&nbsp;<span><a href='index.php?page=$i' >$i</a></span>";
                    } else {
                        echo "&nbsp;&nbsp;<span>$i</span>";
                    }
                }
                if ($page == $totalPages) {
                    echo "&nbsp;&nbsp;<span>Next ></span>";
                } else {
                    $j = $page + 1;
                    echo "&nbsp;&nbsp;<span><a href='index.php?page=$j'>Next</a></span>";
                }
            }
            ?>
        </th>
    </tr>
</table>

谁能告诉我我错过了什么...... :)

4

2 回答 2

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;
        }
?>

希望这对你有用......!!! :)

于 2013-01-15T06:53:12.897 回答
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);
    }
于 2013-01-15T06:09:18.627 回答