• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Pagination Help?

#1
[eluser]Corey Freeman[/eluser]
Sorry if I'm such a newb...I finally figured out models and am attempting to paginate a list of players for my browser game. This is what I ended up with for my controller:

Code:
<?php
class Players extends Controller {

    function Players()
    {
        parent::Controller();
        $this->load->library('pagination');
    }
    
    function index()
    {
        redirect('players/list_players/');
    }
    function list_players() {
        $this->load->model('players_model');
        $per_page = 1;
        $total = $this->players_model->count_players();
        $data['players'] = $this->players_model->get_players($per_page, $this->uri->segment(3));
        $base_url = site_url('players/list');
        $config['base_url'] = $base_url;
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '3';
        $this->pagination->initialize($config);
        $data['title'] = 'Richest Bloggers';
        $this->db->order_by('revenue', 'desc');
        $data['query'] = $this->db->get('players');
        $this->load->view('players', $data);
    }
}
?>

(there are only two players in the database, that's why the number of results is so low.) I end up with a 404 error when I click the next button. Help?

#2
[eluser]umefarooq[/eluser]
hi just found what's the problem in you code your base_url for controller function is wrong

Code:
change it to

$base_url = site_url('players/list_players');

will work

#3
[eluser]Corey Freeman[/eluser]
[quote author="umefarooq" date="1278235661"]hi just found what's the problem in you code your base_url for controller function is wrong

Code:
change it to

$base_url = site_url('players/list_players');

will work
[/quote]

You're my hero. Smile I feel like a complete moron now >.>

#4
[eluser]Corey Freeman[/eluser]
Okay for some reason I can't figure out why it won't let me order them...

Code:
function list_players() {
        $this->load->model('players_model');
        $per_page = 20;
        $total = $this->players_model->count_players();
        $data['players'] = $this->players_model->get_players($per_page, $this->uri->segment(3));
        $base_url = site_url('players/list_players');
        $config['base_url'] = $base_url;
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '3';
        $this->pagination->initialize($config);
        $data['title'] = 'Richest Bloggers';
        $this->db->order_by('username', 'asc');
        $data['query'] = $this->db->get('players');
        //Get User's Profile Link
        $base = base_url();
        $profile_link = $base.'players/user_profile';
        $data['profile_link'] = $profile_link;
        $this->load->view('players', $data);
    }

#5
[eluser]umefarooq[/eluser]
for this you have to check your query CI active record preparing correct query or not print your query using $this->db->last_query();

Code:
after this line
$data['query'] = $this->db->get('players');

echo $this->db->last_query();

where you want to sort your result here

$data['players'] = $this->players_model->get_players($per_page, $this->uri->segment(3));

or here

$data['query'] = $this->db->get('players');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.