Welcome Guest, Not a member yet? Register   Sign In
Links within paginated results
#1

[eluser]Jst4Fun[/eluser]
Hi,
I am quite new to CI. I am trying to create a list with pagination. I am using CI's pagination class to accomplish the same. I was following this Pagination tutorial to implement pagination in my project. I am having certain requirements while displaying the results. Even though there are five fields in the database table I only require data of two fields. They are Name and address. The results should also contain two other col which contains link like edit and delete. So resulting html table should be as follows (sorry the alignment is not correct):



SlNo | Name | Address | Edit | Delete
_________________________________________________________
1 | Test 1 | This is Test |Edit | Delete




So I need to display a serial number (Not from database. Automatically incremented by 1). Name and Description from the database (I was able to display those data successfully), Edit is supposed to be a link which would be directed to index.php/userslist/edit/<id> and delete would also be a link like index.php/userslist/delete/<id>. I am not sure how to add SlNo, edit and delete to the paginated results. Here is the code I have done

controller
Code:
&lt;?php
    $config['base_url'] = base_url().'index.php/users/index/';
        $config['total_rows'] = $this->db->count_all('users');
        $config['per_page'] = '10';
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';

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

        $this->load->model('admin_model');
        $data['results'] = $this->admin_model->get_users_list($config['per_page'],$this->uri->segment(3));

        $this->table->set_heading('No', 'Name', 'Address', 'Action');
        
        $this->load->view('admin/users',$data);
?&gt;

model
Code:
public function get_users_list($num, $offset){
        $this->db->select('name, address');
        $query = $this->db->get('users', $num, $offset);    
        return $query;
    }

view

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

I hope someone would help me. Thanks
#2

[eluser]Jst4Fun[/eluser]
I am not sure why I didn't get any response yet. Is there anything that is not clear or is it not possible? Please help me. I am still not able to figure out on how to do this.
#3

[eluser]tonanbarbarian[/eluser]
Not 100% clear what the problem actually is.
It seems to me you are having trouble working out how to create the edit and delete links?
Unfortunately the CI pagination is very simple, and does not support creating links for you.
Most people just create the edit and delete links exactly as you have and force the user to go back to the start of the list afterwards.
Another option though is to extend the pagination class and have it store some information in the session about what page it is currently on. That way after a delete or edit when you come back to the list of paginated records you can still be on the same page you were previously

I have built this myself but I cannot yet show you an example of it because the project I am using it in is not finished and once it is I will be releasing some of the code I used for others to benefit from.
#4

[eluser]Jst4Fun[/eluser]
Thanks for your reply. I made it work by manually looping the data in my view. This method is working fine now.
Now I have a strange problem. I got a admin folder within the controller folder to place the admin related controller files. In one of the controller I have placed pagination. Here is the code that I used
Code:
&lt;?php
$config['base_url'] = base_url().'index.php/admin/emailtpl/index/';
$config['total_rows'] = $this->db->count_all('templates');
$config['per_page'] = '1';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
$data['results'] = $this->admin_model->get_tpl_list($config['per_page'],$this->uri->segment(4));
?&gt;
and in the view i got following code
Code:
&lt;?php echo $this->pagination->create_links(); ?&gt;
Now to my issue. Even though I am getting the forward links I am not able to go backwards. For example if the pagination is like
Quote:<< 1 2 >>
on loading the page I have "2" as link and "1" in disabled status. If I click on to "2" second page is loaded correctly but "1" would be still disabled (also "<<") while 2 is enabled as a link, which means I wont be able to go back to page 1. I am not sure why I am getting such a strange issue. Hope to get a solution to this. Thanks
#5

[eluser]tonanbarbarian[/eluser]
The issue is most likely that the segment to use to determine the current page is now 1 more than it was because you have added the admin segment to the url
So you need to tell the pagination where to look for it
Code:
$config['uri_segment'] = 4;
will probably work for you
#6

[eluser]Jst4Fun[/eluser]
Great, that worked. Thank you very much tonanbarbarian.
#7

[eluser]harman[/eluser]
Thanks it save me

controller/dir/mycontroller/page/1




Theme © iAndrew 2016 - Forum software by © MyBB