Welcome Guest, Not a member yet? Register   Sign In
Struggling with pagination
#1

[eluser]JimmyJ[/eluser]
Hey folks, hopefully someone can help me. I'm really finding it difficult to get pagination working. I've sat here for nearly 16 hours up to now with no joy Sad

My controller looks like:
Code:
function view_clients()
    {
        
        
        $data = array();
        $config = array();
        
        $offset = 0;
        $limit = 20;
        
        $config = array();
        $config['base_url'] = base_url()."admin/view_clients";
        $config['total_rows'] = $this->db->get('users')->num_rows();
        $config['per_page'] = '2';
        $config['num_links'] = 20;
        $config['uri_segment'] = end($this->uri->segments);
        $config['full_tag_open'] = '<p class="pagination">';
        $config['full_tag_close'] = '</p>';
        
        $this->pagination->initialize($config);
    
        
        $this->load->library('table');
        $tbl_template = array ( 'table_open'  => '<table class="table">' );
        $this->table->set_template($tbl_template);
        $this->table->set_heading('ID', 'Name', 'E-Mail', 'Edit', 'Delete');
        
        if($query = $this->admin_model->getUsers($limit, $offset)) {
            $data['records'] = $query;
        }
        
        $data['content'] = 'admin/clients/view';
        $this->load->view('templates/admin', $data);
        
    }

My Model:
Code:
function getUsers($limit, $offset) {
        
        $this->db->select('users.*, user_groups.*')
        ->from('users')
        ->join('user_groups', 'user_groups.group_id = users.group', 'left')
        ->limit($limit, $offset);
        
        return $this->db->get();
            
        //return $query->result();
        
    }

My View:
Code:
&lt;?php echo $this->pagination->create_links(); ?&gt;
&lt;?php echo $this->table->generate(); ?&gt;

It's spitting out my table header ok but with no results and 2 pagination links that don't work. Think I'm having a hard time understanding. When I get my head around it, I should be good to go.

Does anyone have any ideas what's wrong with my code?
#2

[eluser]JimmyJ[/eluser]
Just updated my model with:
Code:
function getUsers($limit, $offset) {
        
        $this->db->select('users.*, user_groups.*')
        ->from('users')
        ->join('user_groups', 'user_groups.group_id = users.group', 'left')
        ->limit($limit, $offset);
        $query = $this->db->get();
            
        return $query->result();
        
    }

Still no luck Sad
#3

[eluser]pickupman[/eluser]
Try this.
Code:
function view_clients()
    {
        
        
        $data = array();              
        $config = array();

        $config['base_url'] = site_url("admin/view_clients/");
        $config['total_rows'] = $this->db->get('users')->num_rows();
        $config['per_page'] = 2;
        $config['num_links'] = 20;
        $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<p class="pagination">';
        $config['full_tag_close'] = '</p>';
        
        $this->pagination->initialize($config);
    
        
        $this->load->library('table');
        $tbl_template = array ( 'table_open'  => '<table class="table">' );
        $this->table->set_template($tbl_template);
        $this->table->set_heading('ID', 'Name', 'E-Mail', 'Edit', 'Delete');
        
        if($query = $this->admin_model->getUsers($config['per_page'], $this->uri->segment(3,0))) {
            $data['records'] = $query;
        }
        
        $data['content'] = 'admin/clients/view';
        $this->load->view('templates/admin', $data);
        
    }
#4

[eluser]JimmyJ[/eluser]
Still nothing Sad

http://cogohub.com/screenshot.jpg
#5

[eluser]JimmyJ[/eluser]
Got it working. Used the code above, but in the view used:
Code:
&lt;?php echo $this->pagination->create_links(); ?&gt;
                <table class="table">
                        <tr>
                                <th class="left">ID</th>
                                <th class="left">Name</th>
                                <th class="left">Group</th>
                                <th>Edit</th>
                                <th>Delete</th>
                        </tr>
                        &lt;?php if(isset($records)) : foreach($records as $r) : ?&gt;
                        <tr>
                                <td>&lt;?php echo $r->id; ?&gt;</td>
                                <td>&lt;?php echo $r->first_name; ?&gt; &lt;?php echo $r->last_name; ?&gt;</td>
                                <td>&lt;?php echo $r->group_name; ?&gt;</td>
                                <td class="centre">&lt;?php echo anchor("admin/edit_user/$r->id#mclients", "Edit"); ?&gt;</td>
                                <td class="centre">&lt;?php echo anchor("admin/delete_user/$r->id", "Delete"); ?&gt;</td>
                        </tr>
                        &lt;?php endforeach; ?&gt;
                </table>


instead of the table generate function.
#6

[eluser]pickupman[/eluser]
If you wanted to use the table class, you need to iterate the result and use:
Code:
$this->table->add_row();

//or pass a query result to generate();
$this->table->generate($query);




Theme © iAndrew 2016 - Forum software by © MyBB