• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Pagination and links

#1
[eluser]babazumbula[/eluser]
Here's my problem
In contoller I have:
Code:
function members_area(){
        $data = array();
        $this->load->model('membership_model');
        
        // pagination
        $this->load->library('pagination');
        $config['base_url'] = base_url().'site/members_area/';
        $config['total_rows'] = $this->db->get('data')->num_rows();
        $config['per_page'] = 3;
        $config['num_links'] = 4;
        $this->pagination->initialize($config);

        if($query = $this->membership_model->select_all()){
        $data['result'] = $query;
        }
        $data['main_content'] = 'members_view';
        $this->load->view('includes/template',$data);
        
    }

In view:
Code:
<table>
            
            &lt;?php if(isset($result)) : foreach($result as $res) : ?&gt;
          
                
                <tr><td class="nas">&lt;?php echo $res->title; ?&gt;</td><td><a class="edit">id ?&gt;">edit</a></td><td><a class="delete">id ?&gt;">delete</a></td></tr>
                <tr><td class="kon">&lt;?php echo $res->contents; ?&gt;</td></tr>
                
                
            &lt;?php endforeach; ?&gt;

            &lt;?php else : ?&gt;

                <p class="nothing">Nothing in db</p>
            &lt;?php endif;?&gt;

            
    </table>
    &lt;?php echo $this->pagination->create_links(); ?&gt;
What this does is dynamically pull out all rows from db and display them on the page. When I load the page for the first time(when I call function members_area()) edit and delete links work well since href is
Code:
http://localhost... /site/edit/$id
and
Code:
http://localhost... /site/delete/$id
but after I click on any pagination link it somehow interfere with edit and delete links,for they just won't work anymore. Now the link points to
Code:
http://localhost.../site/members_area/edit/$id
instead of
Code:
http://localhost... /site/edit/$id
Basically, calling members_area() and
Code:
$config['base_url'] = base_url().'site/members_area/';
should be the same url, or am I wrong? Seem like I don't understand essence. Pagination works well.

#2
[eluser]pickupman[/eluser]
You seem to have multiple issues happening. For pagination you need to get all records to find the total number of rows. After creating pagination, then run another query selecting records based a limit and offset. This causes the pages to load records 0-3 (first page) 4-6 (second page) 7-9 (third page) etc. When referencing links you may find it better to use site_url() instead of base_url(), as it handles whether you use index.php or not.
Code:
function members_area(){
        
        $this->load->model('membership_model');
        
        // pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('site/members_area/');
        $config['total_rows'] = $this->db->get('data')->num_rows(); //total rows in DB
        $config['per_page'] = 3;
        $config['num_links'] = 4;
        $this->pagination->initialize($config);
        $data['links'] = $this->pagination->create_links();

        $data['result'] = $this->membership_model->select_all($config['per_page'],$this->uri->segment(3,0)); //limit, offset
        $data['main_content'] = 'members_view';
        $this->load->view('includes/template',$data);
        
    }

In view:
Code:
<table>
            
            &lt;?php if(isset($result)) : foreach($result as $res) : ?&gt;
          
                
                <tr><td class="nas">&lt;?php echo $res->title; ?&gt;</td><td>&lt;?php echo anchor('site/'.$res->id,'edit','class="edit"'); ?&gt;"></td><td>&lt;?php echo anchor('site/'.$res->id,'delete','class="delete"'); ?&gt;</td></tr>
                <tr><td class="kon">&lt;?php echo $res->contents; ?&gt;</td></tr>
                
                
            &lt;?php endforeach; ?&gt;

            &lt;?php else : ?&gt;

                <p class="nothing">Nothing in db</p>
            &lt;?php endif;?&gt;

            
    </table>
    &lt;?php echo $links; ?&gt;

#3
[eluser]babazumbula[/eluser]
Solved at last!
So much headache for me and the problem was that I hard coded links instead of deploying anchor() function. Now everything works well.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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