dsbqfrr098575666 2014-10-07 01:34
浏览 149
已采纳

PHP MySQL排序顺序ASC / DESC仅用于显示的记录

I have a query that returns close to a 1000 records. Using pagination, I'm showing a 100 records per page. Great...no problem. I can also sort by last name or first name in either ascending of descending order. ok so far. The first page returns records for last name starting with A to C. The problem I'm having is that when I click last name to descend I get records with last name starting with Z. The records at the end of my query, I want to get results going from C to A (what is shown on my first page...repeating the same functionality in each page.

Here is what I got...

$orderColumn        =       'lastName';
$orderDirection     =       'ASC';

if( isset($_POST["oc"]) && $_POST["oc"] !== '' ) {  $orderColumn        = $_POST["oc"]; }
if( isset($_POST["od"]) && $_POST["od"] !== '' ) {  $orderDirection     = $_POST["od"]; }

$per_page = 100;

$query = "SELECT  *  FROM table as t
            LEFT JOIN table_2 as t2 ON t.pk_uID = t2.fk_uID
            LEFT JOIN table_3 as t3 ON t3.fk_utID = t2.pk_utID
            WHERE t3.fk_utID = 7 and t.interviewed = 0";

$result = $db->query($query);
$count    =     mysql_num_rows($result);

$total = ceil($count/$per_page);

if ($_GET['page']) {

    $page = $_GET['page'];

}


$offset = (($page-1)*$per_page);

$query2 = "SELECT firstName as first, lastName as last FROM table
                LEFT JOIN table_2 as t2 ON t.pk_uID = t2.fk_uID
                LEFT JOIN table_3 as t3 ON t3.fk_utID = t2.pk_utID
                WHERE t3.fk_utID = 7 and interviewed = 0 order by $orderColumn $orderDirection LIMIT $offset, $per_page";

$res = $db-> query($query2);

while($row = mysql_fetch_array($res)){ 

echo "<span style='display: inline-block; width: 15%;'>$row[first]</span>";
echo "<span style='display: inline-block; width: 15%;'>$row[last]</span>";

}
  • 写回答

1条回答 默认 最新

  • drtldt55533 2014-10-07 01:57
    关注

    To what I was saying in comment.. BTW I'm on my mobile phone so this may be unformatted and or take a while...

    Select what_you_need
    From
    (   select your_inner_select
        From table t
        LEFT JOIN table_2 as t2 ON t.pk_uID = t2.fk_uID
        LEFT JOIN table_3 as t3 ON t3.fk_utID = t2.pk_utID
        WHERE t3.fk_utID = 7 and interviewed = 0 LIMIT $offset, $per_page
        ORDER BY $orderColumn ASC
    )t 
    order by $orderColumn $orderDirection
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看