如何按降序获取数据?

时间:2013-11-14 06:29:18

标签: pagination

我已经阅读了本网站上的所有内容,但未能找到答案。 我有一个以升序显示的分页。我注意到当我向数据库输入一个新文件时,它们不会在分页结果中首先显示。任何帮助,将不胜感激。这是我的分页脚本:

<?php
include('connect.php'); 

$tableName="resumesearch";      
$targetpage = "searchresumes.php";  
$limit = 5; 

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$LastPagem1 = $lastpage - 1;                    


$paginate = '';
if($lastpage > 1)
{   




    $paginate .= "<div class='paginate'>";
    // Previous
    if ($page > 1){
        $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
    }else{
        $paginate.= "<span class='disabled'>previous</span>";   }



    // Pages

    if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
    {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page){
                $paginate.= "<span class='current'>$counter</span>";
            }else{
                $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
        }
    }
    elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
    {
        // Beginning only hide later pages
        if($page < 1 + ($stages * 2))       
        {
            for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
            $paginate.= "...";
            $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
            $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
        }
        // Middle hide some front and some back
        elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
        {
            $paginate.= "<a href='$targetpage?page=1'>1</a>";
            $paginate.= "<a href='$targetpage?page=2'>2</a>";
            $paginate.= "...";
            for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
            $paginate.= "...";
            $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
            $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
        }
        // End only hide early pages
        else
        {
            $paginate.= "<a href='$targetpage?page=1'>1</a>";
            $paginate.= "<a href='$targetpage?page=2'>2</a>";
            $paginate.= "...";
            for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?       page=$counter'>$counter</a>";}                 
            }
        }
    }

            // Next
    if ($page < $counter - 1){ 
        $paginate.= "<a href='$targetpage?page=$next'>next</a>";
    }else{
        $paginate.= "<span class='disabled'>next</span>";
        }

    $paginate.= "</div>";       


}
 echo $total_pages.' Results';
 // pagination
 echo $paginate;
?>

1 个答案:

答案 0 :(得分:0)

没有指定ORDER BY的查询的顺序是

因此,查询中需要ORDER BY (DESC) - 这意味着必须是定义稳定排序的列或列集。例如,想象一下查询被更新为:

SELECT * FROM $tableName
ORDER BY createdAt DESC, id ASC
LIMIT $start, $limit

这里我猜想有一个createdAt列代表添加项目的日期项目。在id(可能是自动增量PK)上添加排序是为了确保在多个createdAt列具有相同值的情况下的查询稳定性,这可能是高度并发或质量插入情况。

请注意,我只使用ORDER BY id DESC,因为即使id(假设是)自动增量列,它也表示相同的稳定信息排序。数据库是关于信息的。

此外,

  1. MySQL有一种聪明的方式returning the "total count" with a limit和;
  2. 考虑updating the code to use mysqli/PDO and prepared statements