如何将查询结果拆分为多页(分页)和排序查询数据?

时间:2012-10-15 05:09:47

标签: php codeigniter templates activerecord

在我的模型中,我得到了整个order表。

return $this->db->get('order')->result();

在我看来,我做了foreach循环数据。

<?php foreach ($orders as $order) : ?>
<tr>
   <td><?php echo $order->id; ?></td>
   <td><?php echo $order->order_status; ?></td>
   <td><?php echo $order->cart; ?></td>
     . . . 
<?php endforeach; ?>

好的,这就像魅力一样。但是我需要能够在这里安排一些数据。

  1. 我希望能够按id对记录进行排序,以便我可以获得最新的记录
  2. 我想分割结果(分页)说出每25条记录并添加(转到下一页链接)
  3. 我正在使用 Codeigniter 并构建MVC模式。那么他们在CI中的库或者内置函数是否允许我实现这两个目标呢?我感谢所有有用的建议,代码示例和一般指导。提前谢谢您的时间! :)

    修改1

    很抱歉,我没有提到这一点。有人可以举例说明query需要如何构建return $this->db->get('order', 25)->result();并不能完全在这里给出我想要的结果。

4 个答案:

答案 0 :(得分:2)

您可以使用此代码通过分页获取查询结果:

<?php
/*
    Place code to connect to your DB here.
*/
include('config.php');  // include your code to connect to DB.

$tbl_name="";       //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "filename.php";   //your file name  (the name of this file)
$limit = 2;                                 //how many items to show per page
$page = $_GET['page'];
if($page) 
    $start = ($page - 1) * $limit;          //first item to display on this page
else
    $start = 0;                             //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
$prev = $page - 1;                          //previous page is page - 1
$next = $page + 1;                          //next page is page + 1
$lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;                      //last page minus 1

/* 
    Now we apply our rules and draw the pagination object. 
    We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{   
    $pagination .= "<div class=\"pagination\">";
    //previous button
    if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev\">� previous</a>";
    else
        $pagination.= "<span class=\"disabled\">� previous</span>";   

    //pages 
    if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
    {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page)
                $pagination.= "<span class=\"current\">$counter</span>";
            else
                $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
        }
    }
    elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
    {
        //close to beginning; only hide later pages
        if($page < 1 + ($adjacents * 2))        
        {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //in middle; hide some front and some back
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //close to end; only hide early pages
        else
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
    }

    //next button
    if ($page < $counter - 1) 
        $pagination.= "<a href=\"$targetpage?page=$next\">next �</a>";
    else
        $pagination.= "<span class=\"disabled\">next �</span>";
    $pagination.= "</div>\n";       
}
?>

<?php
    while($row = mysql_fetch_array($result))
    {

    // Your while loop here

    }
?>

<?=$pagination?>

在你的css文件中添加:

div.pagination {
padding: 3px;
margin: 3px;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;

text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
    border: 1px solid #000099;

    font-weight: bold;
    background-color: #000099;
    color: #FFF;
}
div.pagination span.disabled {
    padding: 2px 5px 2px 5px;
    margin: 2px;
    border: 1px solid #EEE;

    color: #DDD;
}

希望它有帮助.. 快乐的编码!!

答案 1 :(得分:1)

您可以使用codeIgniter的Pagination类,它非常易于使用,并且可以100%自定义,动态或通过存储的首选项....

http://ellislab.com/codeigniter/user_guide/libraries/pagination.html

通过分页课来帮助你也可以帮助你1)问题...或者你可以在你的查询中使用命令。

答案 2 :(得分:1)

答案 3 :(得分:1)

你可以使用Datatables jquery插件和服务器端实现,下面已经有一个datatables包装类的实现代码点火器是存储库的链接

它被称为Ignited dataTables

Ignited datatables at github

您可以分叉上述存储库并尝试满足您的需求

并且还使用datatables还有另一个优点,你可以指定服务器端调用几乎所有很好的功能

例如在表格中搜索,分页,排序等

编辑: - 查看编辑后,我发现您在服务器端获取数据时遇到问题,这是codeignitor模型函数,将此函数放在模型类中并实例化并调用模型。

传递限制并按列名称进行适当排序

function getResult($limit, $offset, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('id', 'title', 'category', 'rating', 'price');

            // set default column name to sortby as per your need 
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'title';

        // results query  
            // Change column names as per your need 

        $q = $this->db->select('id, title, category,  rating, price')
            ->from('Order')
            ->limit($limit, $offset)
            ->order_by($sort_by, $sort_order);

        $ret['rows'] = $q->get()->result();

        // count query
        $q = $this->db->select('COUNT(*) as count', FALSE)
            ->from('Order');

        $tmp = $q->get()->result();

        $ret['num_rows'] = $tmp[0]->count;

        return $ret;
    }
相关问题