0

先日、ネットで見つけた素晴らしいチュートリアルを使用して、CI にページネーションを実装することができました。翌日、ソートがチュートリアルでカバーされていないことに気付きました。それで私はソートを実装し続け、それをうまく実装しました。ただし、ページネーション リンクに「sortfield」と「sortorder」に関連する特定の URI セグメントが含まれていないという問題が発生し、混乱が生じました。

すべてのページで保持されるように並べ替えを実装する方法を誰かが知っている場合は、お知らせください。

私のテスト URL : http://zzz.zzz.z.zz/frog/index.php/questions/page/1/id/desc 2 ページをクリックしても保持されない

私のコード:

function page($offset = 0,$sortfield=null,$order=null) {
/**
* Removed all the unnecessary code
*/
    $config = array (
                    'base_url' => base_url () . 'index.php/questions/page/',
                    'total_rows' => $this->db->count_all ( 'questions' ),
                    'per_page' => $this->input->get ( 'perpage' ) ? $this->input->get ( 'perpage' ) : (@$layoutspecificpagination->per_page ? $layoutspecificpagination->per_page : 5),
                    'num_links' => 5,
                    'reuse_query_string' => true
            );

    $config ['total_rows'] = $this->questionsm->read ( $config ['per_page'], $offset, true );

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

次に、ビューで:

<?php echo $this->pagination->create_links(); ?>
4

2 に答える 2

1

base_url を変更し、並べ替えオプションの URL を確認するだけです。これがあなたのコントローラーです

function page($offset = 0,$sortfield='DESC',$order='id') {
 /**
* Removed all the unnecessary code
 */


 if ($this->uri->segment(4)) {
    $order = $this->uri->segment(4);
  }

 if ($this->uri->segment(3)) {
   $sortfield = $this->uri->segment(3);
 } 

$config = array (
                'base_url' => base_url () . 'index.php/questions/page/'.$sortfield.'/'.$order,
                'total_rows' => $this->db->count_all ( 'questions' ),
                'per_page' => $this->input->get ( 'perpage' ) ? $this->input->get ( 'perpage' ) : (@$layoutspecificpagination->per_page ? $layoutspecificpagination->per_page : 5),
                'num_links' => 5,
                'reuse_query_string' => true
        );

$config ['total_rows'] = $this->questionsm->read ( $config ['per_page'], $offset, true );

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

id はデフォルトの並べ替えの DESC にあります

于 2015-06-29T07:07:20.287 に答える
0

答えを得るために次のことを行いました。

  1. 関数に渡されるパラメーターの順序を変更しました

    function page($sortfield=null,$order=null,$offset = 0) {}

  2. @rejoanul-alam の言うように、ベース URL をそのように変更しました

    $config = array ( 'base_url' => base_url () . 'index.php/questions/page/'.$sortfield.'/'.$order.'/', 'total_rows' => $this->db->count_all ( 'questions' ), 'per_page' => $this->input->get ( 'perpage' ) ? $this->input->get ( 'perpage' ) : (@$layoutspecificpagination->per_page ? $layoutspecificpagination->per_page : 5), 'num_links' => 5, 'reuse_query_string' => true );

URI セグメントは既に関数で渡されているため、コントローラーで取得する必要はありませんでした。

  1. それに応じて、ビューのソート テーブルの見出しを変更しました。
于 2015-06-29T08:25:17.377 に答える