1

codeigniter ページネーション クラスの実装に問題があります。ニュース記事を取得するためのモデル、ビュー、およびコントローラーを作成しました。データはビューに正常にエコーされます。

私の問題は、ページネーションを実装しようとすると、データベース内のフィールドの正しい数を取得できないように見えることです。誰かが私が間違ったことを教えてもらえますか?

ページネーション リンクは完全に表示されますが、エコーされるコンテンツは制限されているようには見えません。クエリの行をカウントするにはどうすればよいですか?

ページネーションに必要なクラスは自動ロードされます

モデル:

class News_model extends CI_model {


    function get_allNews()
    {
        $query = $this->db->get('news');

                    foreach ($query->result() as $row) {
        $data[] = array(
             'category' => $row->category,
            'title' => strip_tags($row->title),
            'intro' => strip_tags($row->intro),
            'content' => truncate(strip_tags( $row->content),200),
              'tags' => $row->tags

        );
    }

    return $data;
    } 

コントローラ

      // load pagination class
    $config['base_url'] = base_url().'/news/index/';
    $config['total_rows'] = $this->db->get('news')->num_rows();
    $config['per_page'] = '5';
  $config['full_tag_open'] = '<div id="pagination">';
            $config['full_tag_close'] = '</div>';

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

$viewdata['allnews'] = $this->News_model->get_allNews($config['per_page'],$this->uri->segment(3));

意見

  <?php if (isset($allnews)): foreach ($allnews as $an): ?>
               <?php echo heading($an['title'], 2); ?>
                <?php echo $an['content']; ?>
            <?php endforeach;
        else: ?>
            <h2>Unable to load data.</h2>
        <?php endif; ?>
            <?php echo $this->pagination->create_links(); ?>
4

1 に答える 1

2

コントローラーでは、パラメーターを get_allNews メソッドに渡していますが、メソッドはそれらのパラメーターを使用しません。

$viewdata['allnews'] = $this->News_model->get_allNews($config['per_page'],$this->uri->segment(3));

したがって、すべてのレコードを取得し、限られた結果セットを期待しています。get_allNews メソッドの先頭を次のように変更する必要があります。

class News_model extends CI_model {

// make use of the parameters (with defaults)
function get_allNews($limit = 10, $offset = 0) 
{
    // add the limit method in the chain with the given parameters
    $query = $this->db->limit($limit, $offset)->get('news'); 
    // ... rest of method below
于 2011-11-01T13:07:06.873 に答える