0

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データであり、一貫性がありません。多分問題はビューにもあります。私は何をすべきか?どうもありがとうございました。

4

3 に答える 3

0

オフセットを正しく計算していません。あなたはこれを試すことができます:

$offset = ( $this->uri->segment(3) - 1 ) * $config['per_page']; 

$data['detail'] = $this->buku_model->getBuku($config['per_page'], $offset);
于 2012-04-25T14:15:04.057 に答える
0

codeigniter Pagination Library https://stackoverflow.com/a/10123424/876117を使用して、適切なページネーションについてこれを見たいと思うかもしれません

リクエストごとにすべてのデータを取得できるわけではありません。ページネーション ライブラリによって生成されたリンクのオフセットを指定して、データのみをフェッチする必要があります。

于 2012-04-25T17:34:25.690 に答える
0

おい、コントローラーにはたくさんのクエリがあります。そしてモデル(コルセの)にも。

最初に: コントローラーでほぼ関数のみを使用するようにします。モデルで作成する関数。それが正しいMVCの方法です。

そして、ページネーションの問題について:

GET 変数の使用と悪用を試みます。次のような URL を渡すことができます: http://site.com/controller/function?p=2

見る?"?p=2"? そうすれば、リストの 2 ページ目を表示したいことをコントローラーに伝えることができます。ご存知のように、次のような GET 変数をさらに渡すことができます: http://site.com/controller/function?p=2&cat=romance&year=2010

これらの変数を操作する方法は次のとおりです。

2 番目の例を見てみましょう ("?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: この例では、「get_by_search」という関数を保持するモデル「book」をロードしました。コードを機能させるには、それを行う必要があります。

ヒント: 検索で取得できる変数の数はわかりません。したがって、これらの可能な変数を受け取るいくつかのモデル関数を作成できます。見てください: //モデルブック

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.
}    

お役に立てば幸いです。お伝えしたいことはまだまだありますが、コードの進捗状況について詳しく教えていただければと思います。この答えは、ソリューションのほんの一部です。見る?それはそれほど単純ではありませんが、私はその方法を知っています。さらにサポートが必要な場合は、ここでお知らせください。

うーん、アブラソ!

于 2012-09-01T03:11:03.153 に答える