Welcome Guest, Not a member yet? Register   Sign In
Pagination error in CI
#1

[eluser]_just_me[/eluser]
Hello,

I'm having an issue with pagination in that the links are not displayed. The problem occurred as soon as I added the $num and $offset vars into the model, I'm guessing that my syntax is incorrect and would appreciate it if someone glances their eye over it and can't point me in the right direction.

The model in question is:
Quote: function getAllMusic($num,$offset){
$this->db->select('ae_music.*, ae_genres.*, ae_users.*, ae_locations.*');
$this->db->from('ae_music', 'ae_genres', 'users');
$this->db->join('ae_users', 'ae_users.id = ae_music.user_id');
$this->db->join('ae_genres', 'ae_genres.genre_id = ae_music.genre_id');
$this->db->join('ae_locations', 'ae_locations.location_id = ae_music.location_id');
$this->db->order_by('music_id', 'DESC');
$this->db->limit($num,$offset);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach($q->result() as $getAllMusicRow) {
$data[] = $getAllMusicRow;
}

return $data;
}
else {
return FALSE;
}
}

It works fine in displaying all records and the pagination links without this line:
Code:
$this->db->limit($num,$offset);
Thanks!

_just_me
#2

[eluser]InsiteFX[/eluser]
What you need to show is your pagination code!

InsiteFX
#3

[eluser]_just_me[/eluser]
Ooops, sorry - was a long night.

Here's the controller

Code:
function index()
        {
            $this->load->model('data_model');
            $this->load->library('pagination');

            $config['per_page'] = '2';
            $config['base_url'] = base_url().'index.php/music/';
            $config['full_tag_open'] = '<p>';
            $config['full_tag_close'] = '</p>';
            
            $getAllMusicRow = $this->data_model->getAllMusic($config['per_page'],$this->uri->segment(3));                            
            $getAllGenresRow = $this->data_model->getAllGenres();                
            $getAllLocationsRow = $this->data_model->getAllLocations();                            
            $getTopMusicRow = $this->data_model->getTopMusic();            
            $getLatestSixRow = $this->user_model->getLatestSix();            
                        
            $totalRows = count($getAllMusicRow);                        
            
            $config['total_rows'] = $totalRows;                        
            $this->pagination->initialize($config);

            $data['pagination'] = $this->pagination->create_links();
            $data['getAllMusicRow'] = $getAllMusicRow;                                        ;
            
            $this->template->write('pageTitle', 'Music');
            $this->template->write_view('leftContent', 'music/music_view', $data);
            $this->template->write_view('midContent', 'common/mid_column_one', $data);
            $this->template->write_view('rightContent', 'common/right_column_one', $data);
            $this->template->render();
        }

and here's the pertinent bit from the view:

Code:
&lt;? echo $pagination; ?&gt;
&lt;? if(is_array($getAllMusicRow)) : ?&gt;    
    <ol>
    &lt;? foreach($getAllMusicRow as $row) : ?&gt;                                    
        <div class="musicRow">
        <li>
            <p class="music_name">"&lt;? echo $row->music_name ;?&gt;"</p>
            <p class="user_name">by &lt;? echo anchor ('people/profile/'.$row->user_name_url, $row->user_name, 'class="simple"') ?&gt;</p>
            <p class="location_name">from &lt;? echo anchor ('places/city/'.$row->location_url_name, $row->location_name, 'class="simple"') ?&gt;</p>
            <p class="genre_name">genre &lt;? echo anchor ('music/genre/'.$row->genre_url_name, $row->genre_name, 'class="simple"') ?&gt;</p>  
            <div class="play">play</div>
            <div class="info">&lt;? echo anchor ('music/track/'.$row->music_url_name.'/'.$row->music_id, 'details') ?&gt;</div>
        </li>
        </div>                                        
    &lt;? endforeach; ?&gt;
    </ol>
&lt;? else : ?&gt;
    <h5>There should always be something here, but just in case...</h5>
&lt;? endif; ?&gt;

Thanks,
Matt
#4

[eluser]InsiteFX[/eluser]
You need to add this for one:
Code:
$config['uri_segment'] = 3;

InsiteFX
#5

[eluser]_just_me[/eluser]
Thanks for the response.

I've added the config option in, but still have no joy in getting the links to display.

When the limit() line is moved from the model, the links reappear but then of course it's not limiting the results. Completely stumped.

Thanks,
_just_me
#6

[eluser]InsiteFX[/eluser]
Pagination with CodeIgniter

InsiteFX
#7

[eluser]_just_me[/eluser]
Just in case anyone stumbles across this - I was stupidly populating the per_page with a count from the query that was grabbing the data, instead of running a separate count_all query.

Thanks for the link InsiteFX.

_just_me
#8

[eluser]InsiteFX[/eluser]
Your very Welcome.

Pagination is a little confusing at first but once you get use to it no problem.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB