当我在 CodeIgniter 中使用分页时,数据量不一致


inconsistent amount of data when I use pagination in CodeIgniter

我想使用 CodeIgniter 中的分页库执行大量数据。我一直在实施它并且它有效。但是,我有一个问题 - 每页的数据量不一致。

这是我如何制作的简单代码...

控制器

class Buku_con extends Controller {
    public function Buku_con() {
        parent::__construct();
        $this->load->model('buku_model');
        $this->load->library('pagination'); //call pagination library
    }
    function getBuku() {
        //count the total rows of tb_book
        $this->db->select('*');
        $this->db->from('tb_book');
        $getData = $this->db->get('');
        $a = $getData->num_rows();
        $config['base_url'] = base_url().'index.php/Buku_con/getBuku/'; //set the base url for pagination
        $config['total_rows'] = $a; //total rows
        $config['per_page'] = '10'; //the number of per page for pagination
        $config['uri_segment'] = 3; //see from base_url. 3 for this case
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $this->pagination->initialize($config); //initialize pagination
        $data['detail'] = $this->buku_model->getBuku($config['per_page'],$this->uri->segment(3));
        $this->load->view('buku_view', $data);
    }
}

class Buku_model extends Model {
    function Buku_model() {
        parent::Model();
    }
    function getBuku($perPage,$uri) { //to get all data in tb_book
        $title=$this->session->userdata('title');
        $this->db->select('*');
        $this->db->from('tb_book');
        $this->db->where('title','$title');
        $this->db->order_by('id','DESC');
        $getData = $this->db->get('', $perPage, $uri);
        if($getData->num_rows() > 0)
            return $getData->result_array();
        else
            return null;
    }
}

视图

if(count($detail) > 0) { 
//... html for table .....
foreach($detail as $rows) {
    echo 
    .... $rows['id'] .....
    .... $rows['title'] .....
    .... $rows['author'] .....
    ....
} 
echo $this->pagination->create_links(); ....

分页效果很好,但每页的数据量并不像我在控制器中定义的那样一致。我认为问题是由我在模型中使用的查询引起的 - 标题会话。

我希望数据每

页执行 10 个数据,但第 1 页只有 5 个数据,第 2 页只有 4 个数据 - 不一致。也许问题也出在观点上。我该怎么办?谢谢。

您没有正确计算偏移量。你可以试试这个:

$offset = ( $this->uri->segment(3) - 1 ) * $config['per_page']; 
$data['detail'] = $this->buku_model->getBuku($config['per_page'], $offset);

您可能想使用codeigniter分页库查看此内容以进行正确的分页https://stackoverflow.com/a/10123424/876117

您不会获取每个请求的所有数据。您只需要获取分页库生成的链接中指定的偏移量数据。

伙计,你的控制器上有很多查询。还有模型(胸衣)。

首先:尝试几乎只使用控制器上的函数。在模型上创建的函数。这是正确的MVC方式。

关于您的分页问题:

尝试使用和滥用GET变量。善良,你可以传递这样的网址:http://site.com/controller/function?p=2

看?"?p=2"?这样我就可以对我的控制器说我想看到列表的第二页。你知道,我可以传递更多的GET变量,比如:http://site.com/controller/function?p=2&cat=romance&year=2010

您将如何处理这些变量:

让我们以第二个示例为例("?p=2&cat=romance&year=2010")

$page = $this->input->get('p'); // this is more secure too. avoid use $_GET or $_POST
$category = $this->input->get('cat');
$year = $this->input->get('year');

现在,您可以使用一些函数来获取与值匹配的书籍列表:

$data['books'] = $this->book->get_by_search($page, $category, $year);

PS:在这个例子中,你加载了一个模型"book",其中包含一个名为"get_by_search"的函数。您必须这样做才能使代码正常工作。

提示:我们永远不知道搜索可以获得多少变量。因此,您可以创建一些模型函数来接收这些可能的变量。看:模型书

public function get_by_search($page = NULL, $cat = NULL, $year = NULL, $author = NULL)
{
    $perpage = 10;
    // the NULL is to prevent an error if the function is called without them.
    if ( ! is_numeric($page) || $page < 0)
    {
        $qtd    = $perpage;
        $offset = 0;
    }
    else
    {
        $qtd    = $page * perpage;
        $offset = $perpage;
    }
    $this->db->where('category', $cat);
    $this->db->where('year', $year);
    return $this->db->get('books', $perpage, $offset);
    // if you want to debug your queries, do the same above, but with no return
    // and then use this:
    echo this->db->last_query(); die; // stop here and see the query string.
}    

好吧,我希望它能帮助你。还有更多要告诉你的,但我认为你最好告诉我更多关于你的代码进度。这个答案只是解决方案的一部分。看?这不是那么简单,但我知道怎么做。如果您需要更多帮助,请在此处告诉我,好吗?

乌姆阿布拉索!