douyun8674 2013-01-15 05:32
浏览 20
已采纳

按列名排序在列表页面上的普通php中不起作用

I have created listing page with pagination and sorting by column name in . Pagination is working fine but search by column name is not working as per expected. Here is my code :

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

//Column name with link for sorting records

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

//Pagination code

    <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>

Can anyone tell me what I am missing.... :)

  • 写回答

2条回答 默认 最新

  • duanjiao4763 2013-01-15 06:09
    关注
        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条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线