Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter pagination pulling item more then once

This is a very irritating issue. I have my codeigniter pagination set up and so I thought working, but looking at it closer it seems that on the last page it's pulling in previous results to fill the page in.

So say I want ten per page and have fourteen results. The first page has ten results, and so does the second. When it should be the first has ten and the second has four. It would be fine if it was just repeating one result, but it's irritating to have to scroll through six previous results. Any help would be much appreciated.

in my controller I have the pagination code

$config = array();
$config["base_url"] = base_url()."myStories/".$id;
$config["total_rows"] = $this->data_model->my_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config['num_links'] = 2;
$choice = $config["total_rows"] / $config["per_page"];
//$config["num_links"] = round($choice);

$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

$this->load->view('userStory_view', array(
    'query' => $this->data_model->pullMyStories($config['per_page'], $page),
    'links' => $this->pagination->create_links(),
    'user' => $this->users_model->getUser($this->user->user_id),
)); 

and then in my model I have the count and then the actual results coming back

public function my_count() {
        //This counts all the stories that belong to that author
        $author = $this->uri->segment(2);
        $this->db->where('author', $author);
        $this->db->where(array('approved !=' => 'd'));
        $query = $this->db->get('story_tbl');
        return $query->num_rows();
       }

    public function pullMyStories($limit, $start){
        //This pulls back all the stories that belong to that author
        $this->db->limit($limit, $start);
        $this->db->order_by("date", "desc");
        $author = $this->uri->segment(2);
        $this->db->where(array('approved !=' => 'd'));
        $this->db->where('author', $author);
        $story = $this->db->get('story_tbl');
        return $story->result();    
    }

the route I have set up that does work

$route['myStories/(:any)'] = "story/viewStories/$1";

I thought initially that my count was count was wrong, but even with a count of 14, 20 results come back.

For further information I am more than positive that my baseUrl is correct. I have modified my .htaccess to get rid of the index.php and have edited my route file to make the controller disappear from the url. To try and make it easy to remember for the user.

I am also very sure that the uri segments are correct. If they were not correct then my page would not be coming up at all.

I have tried all the normal solutions and nothing has worked. That is why I am asking here and why I have placed a bounty on this question.

like image 255
zazvorniki Avatar asked Oct 22 '22 05:10

zazvorniki


2 Answers

var $base_url           = ''; // The page we are linking to
var $prefix             = ''; // A custom prefix added to the path.
var $suffix             = ''; // A custom suffix added to the path.

var $total_rows         =  0; // Total number of items (database results)
var $per_page           = 10; // Max number of items you want shown per page
var $num_links          =  2; // Number of "digit" links to show before/after the currently viewed page
var $cur_page           =  0; // The current page being viewed
var $use_page_numbers   = FALSE; // Use page number for segment instead of offset
var $first_link         = 'First';
var $next_link          = '->';
var $prev_link          = '<-';
var $last_link          = 'Last';
var $uri_segment        = 2;
var $full_tag_open      = '';
var $full_tag_close     = '';
var $first_tag_open     = '';
var $first_tag_close    = ' ';
var $last_tag_open      = ' ';
var $last_tag_close     = '';
var $first_url          = ''; // Alternative URL for the First Page.
var $cur_tag_open       = '&nbsp;<strong>';
var $cur_tag_close      = '</strong>';
var $next_tag_open      = '&nbsp;';
var $next_tag_close     = '&nbsp;';
var $prev_tag_open      = '&nbsp;';
var $prev_tag_close     = '';
var $num_tag_open       = '&nbsp;';
var $num_tag_close      = '';
var $page_query_string  = FALSE;
var $query_string_segment = 'per_page';
var $display_pages      = TRUE;
var $anchor_class       = '';
like image 83
Rachael Avatar answered Oct 26 '22 10:10

Rachael


Your problem is that you are passing the wrong parameters to your pullMyStories method. On the first page you will be apply the following limit to your query

LIMIT 0,10

Then on the second page

LIMIT 1,10

Then on the third

LIMIT 2,10

So you pagination is only moving forward one item at a time instead of ten. So you need to change this

'query' => $this->data_model->pullMyStories($config['per_page'], $page),

To this

'query' => $this->data_model->pullMyStories($config['per_page'], ($page * $config['per_page'])),
like image 28
Pattle Avatar answered Oct 26 '22 09:10

Pattle