0
votes

I'm using codeigniter 2.2.x version. Basically I have a few posts in my database and I show 2 per page. As all of you know pagination renders from 1 to total page numbers and when clicking on let's say 3, the url should be something like blog/pages/3 but mine goes to 4, 4 goes to 6, 5 goes to 8 and so on. The relation is like; if pagination number is x, and the one shows on url is y, y=2*x-2 ... Here are my config settings (controller):

    function index()
{
    $this->load->Model('sayfalama_model', 'Model');

    $this->load->library('pagination');

    $data['sayfalama_linkleri'] = $this->sayfalama_linkleri($this->Model->yazilar_adet());

    $data['yazilar'] = $this->Model->yazilar($this->uri->segment(3,0),2);
    $data['title'] = 'Blog Arşivi';

    $this->load->view('templates/header', $data);
    $this->load->view('templates/menu', $data);
    $this->load->view('blog/index', $data);
    $this->load->view('templates/footer', $data);
}

function sayfalama_linkleri($toplam)
{
    $config = array(
        'base_url'          => site_url('blog/sayfa'),
        'first_url'         => site_url('blog/sayfa/1'),
        'total_rows'        => $toplam,
        'per_page'          => 2,
        'page_query_string' => FALSE,
        'use_page_numbers'  => FALSE,
        'num_links'         => 2,
        'uri_segment'       => 3,
        'full_tag_open'     => '<ul class="pagination pull-right">',
        'full_tag_close'    => '</ul>',
        'first_link'        => 'İlk Sayfa',
        'first_tag_open'    => '<li>',
        'first_tag_close'   => '</li>',
        'last_link'         => 'Son Sayfa',
        'last_tag_open'     => '<li>',
        'last_tag_close'    => '</li>',
        'next_link'         => '&raquo;',
        'next_tag_open'     => '<li>',
        'next_tag_close'    => '</li>',
        'prev_link'         => '&laquo;',
        'prev_tag_open'     => '<li>',
        'prev_tag_close'    => '</li>',
        'cur_tag_open'      => '<li><span class="current">',
        'cur_tag_close'     => '</span></li>',
        'num_tag_open'      => '<li>',
        'num_tag_close'     => '</li>'

    );

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

    return $this->pagination->create_links();
}

and this is my model:

class Sayfalama_model extends CI_Model{



function yazilar($baslangic, $limit)
{
    $sql = "SELECT * FROM news ORDER BY id ASC LIMIT $baslangic,$limit";
    $query = $this->db->query($sql);

    if( $query->num_rows() > 0 )
    {
        return $query->result_array();
    }
    else
    {
        return FALSE;
    }
}

function yazilar_adet()
{
    $sql = "SELECT COUNT(*) as adet FROM news";
    $query = $this->db->query($sql);

    return (int)$query->row()->adet;
}

}

Just in case, will share the view, too.

<?php if( $yazilar ) : ?>
     <?php foreach ($yazilar as $dizi): ?>

        <?php 
        $kisa_metin = mb_substr($dizi['text'],0,85);
        echo'
        <div class="col-md-8">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title">'.$dizi['title'].'</h3>
            </div>
            <div class="panel-body">
                    '.$kisa_metin.'...<br><small class="pull-right"><a href="'.base_url().'blog/'.$dizi['slug'].'"> Devamını Oku...</a></small>
            </div>
        </div>
        </div> <br clear="both">'; ?>
        <?php endforeach ?>
            <div class="row">
                    <?php echo $sayfalama_linkleri; ?>
            </div>
    <?php endif; ?>

Note: When I change 'use_page_numbers'to true, page numbers are correct,but I'm losing some of my data. Some of the posts don't show up. Help a man out, please. Thanks all.

1

1 Answers

0
votes

I tried and found out a solution. It turned out to be pretty easy. I changed _use_page_numbers_ to true and instead of $this->uri->segment(3,0) I wrote ($this->uri->segment(3,0)*2)-2 . It worked out without a problem. Note that for page 1 and 2 we'll have to use an if else and when $this->uri->segment(3,0) is less than 2 $this->uri->segment(3,0) will remain same. And we'll have to configure everything depending on posts per page if it's other than 2 unlike my case.