2012-01-28 18 views
5

私のコントローラ機能ページネーションが正しい表示ページ番号CodeIgniterの

function test($start_from = 0) 
{ 
    $this->load->library('pagination'); 

    $data = array(); 

    $per_page = 3; 
    $total = $this->activity_model->count_by(); 

    $config['base_url'] = base_url() . 'test'; 
    $config['total_rows'] = $total; 
    $config['per_page'] = $per_page; 
    $config['uri_segment'] = 2; 
    $config['num_links'] = 2; 
    $config['use_page_numbers'] = TRUE; 

    $data['follow'] = $this->activity_model->get($per_page, $start_from); 

    $this->pagination->initialize($config); 

    $data['pagination'] = $this->pagination->create_links(); 

    $this->load->view('front_end/test' ,$data); 
} 

私のルートをしないでください。

$route['test'] = "user_activity/test"; 
$route['test/(:any)'] = "user_activity/test/$1"; 

モデル:

function get($limit,$start_from) 
{ 
$sql = "SELECT * FROM user_follow LIMIT $start_from, $limit"; 

    $query = $this->db->query($sql); 
    return $query->result_array(); 
} 

問題は、私はページネーション1,2を持っているということです3,4,5、そしてすべてのページに3つの項目が表示されます。私は、3ページのURLショー6をクリックして、その上でとき、私は2番目のページのURLショー3 をクリックすると、URLで、それは私のページ番号1,2,3,4,5

を示すことをやりたい+3

それは可能ですが、私はインターネット上で助言を探して時間を費やしますが、私は[config] [config] [config_] [TRUE]; [/ code]

多分あなたはどのライブラリにアドバイスできますか?

答えて

7

オフセットの代わりにページ番号を使用するように、ページ分類クラス(/system/libraries/Pagination.php)で次の変更を行います。

OLD(ライン146-153):

if ($CI->uri->segment($this->uri_segment) != 0) 
{ 
    $this->cur_page = $CI->uri->segment($this->uri_segment); 

    // Prep the current page - no funny business! 
    $this->cur_page = (int) $this->cur_page; 
} 

NEW:

if文のデフォルトがあることを確認するために「他」オプションを追加します。ページ= 1

if ($CI->uri->segment($this->uri_segment) != 0) 
{ 
    $this->cur_page = $CI->uri->segment($this->uri_segment); 

    // Prep the current page - no funny business! 
    $this->cur_page = (int) $this->cur_page; 
} 
else 
{ 
    $this->cur_page = 1; 
} 

OLD(ライン175):

$this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

NEW:

現在のページは、コントローラ/ URIに従うように、単純にこの行をコメントアウトします。

//$this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

OLD(ライン206):

$i = $uri_page_number - $this->per_page; 

NEW:

前のページが常に現在のページが1減算されるべきである

$i = $uri_page_number - 1; 

OLD (ライン230):

if ($this->cur_page == $loop) 

NEW:改ページが欠落し

URIは、ページ1考慮すべきです。

if ($this->cur_page == $loop || ($this->cur_page == 1 && $this->cur_page == $loop)) 

OLD(ライン238から247):

if ($n == '' && $this->first_url != '') 
{ 
    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close; 
} 
else 
{ 
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix; 

    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close; 
} 

NEW:

ページのURLは、ページ番号ではなくオフセットを使用する必要があります。

if ($n == '' && $this->first_url != '') 
{ 
    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$loop.'">'.$loop.'</a>'.$this->num_tag_close; 
} 
else 
{ 
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix; 

    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$loop.'">'.$loop.'</a>'.$this->num_tag_close; 
} 

OLD(ライン256):

$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close; 

NEW:

次のページには、常に現在のページの合計と1

$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page + 1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close; 

する必要があります旧式(ライン262):

$i = (($num_pages * $this->per_page) - $this->per_page); 

NEW:

最後のページは、ページの合計数でなければなりません。

$i = $num_pages; 

すべての古い行を新しい行に置き換えます。変更する前に必ずファイルのバックアップを取ってください。

希望はこのことができます:)

編集:私は$per_page * ($start_from-1)で新しい変数$開始を追加した。ここ

function test($start_from = 0) 
{ 
    $this->load->library('pagination'); 

    $data = array(); 

    $per_page = 3; 
    $total = $this->activity_model->count_by(); 

    $config['base_url'] = base_url() . 'test'; 
    $config['total_rows'] = $total; 
    $config['per_page'] = $per_page; 
    $config['uri_segment'] = 2; 
    $config['num_links'] = 2; 
    $config['use_page_numbers'] = TRUE; 

    $start = $per_page * ($start_from-1); 

    $data['follow'] = $this->activity_model->get($per_page, $start); 

    $this->pagination->initialize($config); 

    $data['pagination'] = $this->pagination->create_links(); 

    $this->load->view('front_end/test' ,$data); 
} 

:のようなあなたのコントローラ機能テストを更新する必要が

。今、この$ startを引数としてモデルに渡します。ページごとのアイテムが10であり、あなたが2番目のページ10を与える$start = 10 *(2-1)上にある場合は、このDOはしてページあたりの項目数を掛けている何

(現在のページ番号は-1).Thisを意味します。だからあなたの結果は、10,20からスタートしますので、1人の

希望はこのことができます:)

+0

で私をつぶやくクエリに対してこのことができます

if (isset($result)) { $isArray = is_array($result) ? "YES" : "NO"; //check that result is an array or not as per your requirement. if ($isArray == "YES") { echo "show your stuffs here.."; } else{ echo "display your message instead for php error here.."; } } 

希望... をこれは、CodeIgniterの2.0.3のために行くのだろうか?とにかくありがとうございました ! – Viktors

+0

行番号に若干の変更があります。どんな方法でも試してみてください。私のものは少し古いバージョンです。それが動作する場合私にお知らせください:) – Sabari

+0

これはあなたのために働くか? – Sabari

11

私は、クラスを変更せずにこれを行うことができました。最良の方法は、ページ区切りクラスのコピーを作成し、変更して使用することです。この方法でCIを更新すると、変更内容が失われることはありません。ここで私の解決策は、クラスを変更せずにです。

最初に、設定オプション$config['use_page_numbers'] = TRUEだけを使用すると、そのトリックは実行されますが、完全ではないと言いたいと思います。
URLバーページを手動で編集しようとすると、ページのようにオフセットされていないように扱われます。また、ページ2からページ1に戻ろうとすると、次のようになります。 "prev"リンクはページ番号をオフセットのように扱います。

コード:=偽(私/ URL)または(私/ URL /ページ)

$config['base_url'] = base_url('my/url/page'); 
$config['total_rows'] = count($this->my_model->get_all()); 
$config['per_page'] = 2; 
$config['use_page_numbers'] = TRUE;  
$config['uri_segment'] = 4; 

//i'm looading the pagination in the constuctor so just init here 
$this->pagination->initialize($config); 

if($this->uri->segment(4) > 0) 
    $offset = ($this->uri->segment(4) + 0)*$config['per_page'] - $config['per_page']; 
else 
    $offset = $this->uri->segment(4); 
//you should modify the method in the model to accept limit and offset or make another function - your choice  
$data['my_data'] = $this->my_model->get_all($config['per_page'], $offset); 

この方法
ページ - = 0基本的には第四のURIセグメントがfalseの場合、
ページ(私の/ URL /ページ/ 0)

ページ= 1(私/ URL /ページ/ 1)

すべては最初のページが表示され、その後、他のリンクが正常に動作します。私はまた、例えば、ページを検証しています - 誰かが(my/url/page/2323)を入力したい場合、これはエラーをスローし、モデルには結果が偽であるかどうかをチェックしてください。ページまたは何か。お役に立てれば。

+0

これは私の問題を解決したtnx – Ramje

0

誰かが手動でURLからページ番号を入力する場合は、PHPのエラーを回避することができます

例えば私/ URL /ページ/ 786 あなたの結果は、デフォルトのそれによって、クエリ786に表示する何かを持っていない可能性がありますあなたが使用することができ、この避けるerror..to PHP表示されます:Twitterの@sufiyantech

関連する問題