J
J
jack iakovenko2020-12-02 21:02:31
CodeIgniter
jack iakovenko, 2020-12-02 21:02:31

Why isn't all info displayed on the page during pagination?

I have a website for watching movies online. So, I have 52 films in the database, and only 20 are displayed on the page when pagination is 10 per page. I can't understand why. Can anyone suggest what I'm doing wrong?

Below I will insert the codes:

this is the films_model model

<?php

class Films_model extends CI_Model {

    public function __construct() {
        $this->load->database();
    }

    public function getFilms($slug = FALSE, $limit, $type = 1) {
        if($slug === FALSE) {
            $query = $this->db
                ->where('category_id', $type)
                ->order_by('year', 'desc')
                ->limit($limit)
                ->get('movie');

            return $query->result_array();    
        }

        $query = $this->db->get_where('movie', array('slug'=>$slug));
        return $query->row_array();
    }

    public function getFilmsByRating($limit) {
        $query = $this->db
            ->order_by('rating', 'desc')
            ->where('category_id', 1)
            ->where('rating>', 0)
            ->limit($limit)
            ->get('movie');

        return $query->result_array();    
    }

    public function getMoviesOnPage($row_count, $offset, $type = 1) {
        $query = $this->db
            ->where('category_id', $type)
            ->order_by('year', 'desc')
            ->get('movie', $row_count, $offset);

        return $query->result_array(); 
    }
}


and this is the movies controller
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Movies extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function type($slug = NULL) {
        $this->load->library('pagination');

        $this->data['movie_data'] = NULL;

        $offset = (int) $this->uri->segment(4);

        $row_count = 10;

        $count = 0;

        

        if($slug == "films") {
            $count = count($this->films_model->getFilms(0, 1));
            $p_config['base_url'] = '/movies/type/films/';
            $this->data['title'] = "Movies";
            $this->data['movie_data'] = $this->films_model->getMoviesOnPage($row_count, $offset, 1);
        }

        if($this->data['movie_data'] == null) {
            show_404();
        }

        $p_config['total_rows'] = $count;
        $p_config['per_page'] = $row_count;

        $p_config['full_tag_open'] = "<ul class='pagination'>";
    $p_config['full_tag_close'] ="</ul>";
    $p_config['num_tag_open'] = '<li>';
    $p_config['num_tag_close'] = '</li>';
    $p_config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
    $p_config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
    $p_config['next_tag_open'] = "<li>";
    $p_config['next_tagl_close'] = "</li>";
    $p_config['prev_tag_open'] = "<li>";
    $p_config['prev_tagl_close'] = "</li>";
    $p_config['first_tag_open'] = "<li>";
    $p_config['first_tagl_close'] = "</li>";
    $p_config['last_tag_open'] = "<li>";
    $p_config['last_tagl_close'] = "</li>";

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

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

        $this->load->view('templates/header', $this->data);
        $this->load->view('movies/type', $this->data);
        $this->load->view('templates/footer');
    }

    public function view($slug = NULL) {

        $movie_slug = $this->films_model->getFilms($slug, false, false);

        if(empty($movie_slug)) {
            show_404();
        }

        $this->data['title'] = $movie_slug['name'];
        $this->data['year'] = $movie_slug['year'];
        $this->data['rating'] = $movie_slug['rating'];
        $this->data['descriptions_movie'] = $movie_slug['descriptions'];
        $this->data['player_code'] = $movie_slug['player_code'];
        $this->data['director'] = $movie_slug['director'];

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


}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2020-12-02
@Immortal_pony

The approach is generally wrong - to calculate the total number, you need to use SQL_CALC_FOUND_ROWS or SELECT COUNT(*) FROM ( {your-select} ).
CI has built-in wrappers for this: https://codeigniter.com/user_guide/libraries/pagin...
You are trying to pull out all the entries and count their number. However, even this is wrong.
To count the total number of records, you run the code

$count = count($this->films_model->getFilms(0, 1));

Since you are passing $slug=0, the $slug===FALSE test fails and the section is executed
$query = $this->db->get_where('movie', array('slug'=>$slug));

Most likely, such a fix will solve your problem, but in general you need to rewrite it normally.
$count = count($this->films_model->getFilms(false, PHP_INT_MAX));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question