简体   繁体   English

使用SQL Server 2008进行代码点火器分页

[英]Code Igniter pagination with sql server 2008

I'm working with CI pagination helper with my sql 2008 db. 我正在使用SQL 2008数据库使用CI分页助手。

Where my function on the model is: 我在模型上的功能是:

function get_data($limit, $offset) {
    $billing_db = $this -> load -> database('billing', TRUE);

    $billing_db -> select('stuff, stuff2, stuff3');
    $billing_db -> from('mytable');
    $billing_db -> limit($limit, $offset);
    $this -> db -> order_by("id", "asc");
    $q = $billing_db -> get();

    return $q;
}

Now on my controller I called the function like: 现在在我的控制器上,我调用了如下函数:

$data['billers'] = $this -> billing_model -> get_data(10, $this -> uri -> segment(3));

And when I open the page on default it displays 10 entries correctly. 当我打开默认页面时,它将正确显示10个条目。

Then the problem starts when I change the page, lets say I click next. 然后,当我更改页面时问题就开始了,可以说我单击了下一步。
Now the url segment 3 is 10. which should start on the 10th entry and limit by 10. 现在,网址段3为10,该网址应从第10个条目开始,并限制为10。

But whats happening is it starts from entry 1 and displays 20 records. 但是发生的事情是它从条目1开始并显示20条记录。
Everytime the offset goes higher it just displays more records starting from the begining. 每当偏移量增加时,它只会从头开始显示更多记录。

What could be wrong? 有什么事吗

/**
 * Limit string
 *
 * Generates a platform-specific LIMIT clause
 *
 * @access  public
 * @param   string  the sql query string
 * @param   integer the number of rows to limit the query to
 * @param   integer the offset value
 * @return  string
 */
function _limit($sql, $limit, $offset)
{
    $i = $limit + $offset;

    return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
}

The _limit function in sqlsrv_driver and mssql_driver doesn't work properly with offset. sqlsrv_driver和mssql_driver中的_limit函数不能与offset一起正常使用。 I believe this will be fixed once CI 3 is released, but so far the best solution I have found is this: 我相信,一旦发布CI 3,这一问题就会得到解决,但是到目前为止,我发现的最佳解决方案是:

function _limit($sql, $limit, $offset)
{       
    if (count($this->ar_orderby) > 0)
    {
        $OrderBy  = "ORDER BY ";
        $OrderBy .= implode(', ', $this->ar_orderby);

        if ($this->ar_order !== FALSE)
        {
            $OrderBy .= ($this->ar_order == 'desc') ? ' DESC' : ' ASC';
        }
    }

    $sql = preg_replace('/(\\'. $OrderBy .'\n?)/i','', $sql);
    $sql = preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 row_number() OVER ('.$OrderBy.') AS rownum, ', $sql);

    $NewSQL = "SELECT * \nFROM (\n" . $sql . ") AS A \nWHERE A.rownum BETWEEN (" .($offset + 1) . ") AND (".($offset + $limit).")";

    return     $NewSQL;
}

Posted by yangh here: http://ellislab.com/forums/viewthread/160626/ yangh在这里发布: http ://ellislab.com/forums/viewthread/160626/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM