0

在我的模型中,我得到了整张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 条记录并添加(转到下一页链接)

我正在使用Codeigniter并基于 MVC 模式进行构建。那么他们可能是一个库或内置函数,CI可以让我实现这两个目标吗?我感谢所有有用的建议、代码示例和一般指导。提前感谢您的宝贵时间!:)

编辑 1

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

4

4 回答 4

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 �&lt;/a>";
    else
        $pagination.= "<span class=\"disabled\">next �&lt;/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;
}

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

于 2012-10-15T05:55:49.407 回答
1

您可以使用 codeIgniter 的 Pagination 类,它非常易于使用,并且是 100% 可定制的,无论是动态的还是通过存储的首选项......

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

通过分页课程将帮助你解决你的 1) 问题......否则你可以在你的查询中使用 order by。

于 2012-10-15T05:15:21.493 回答
1

您可以使用 codeigniter 自定义库分页查看这些页面来实现此目的。

http://phpmaster.com/pagination-with-codeigniter/

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

于 2012-10-15T05:15:32.923 回答
1

您可以将 Datatables jquery 插件与服务器端实现一起使用,下面已经有一个用于代码点火器的数据表包装器类的实现是存储库的链接

它被称为点燃的数据表

在 github 上点燃数据表

您可以分叉上述存储库并根据您的需要进行尝试

并且使用数据还有另一个优点,您可以指定服务器端调用几乎所有不错的功能

如表格搜索、分页、排序等

编辑:-查看您的编辑后,我发现您在服务器端获取数据时也有问题,这是 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;
    }
于 2012-10-15T05:19:16.130 回答