Welcome Guest, Not a member yet? Register   Sign In
[Solved] codeigniter pagination question
#1

(This post was last modified: 03-23-2015, 04:58 AM by wolfgang1983.)

When I view my table it shows a list of my files controllers. But inside that list I have a $data['controller_files'] = array();

Is it possible to some how make the controller_files array work with the codeigniter pagination.

On the view I have set up foreach ($controller_files as $controllers) {}

$route['admin/extension/permissions'] = "admin/extension/extension_permissions/index";
$route['admin/extension/permissions/(:any)'] = "admin/extension/extension_permissions/index/$1";

I have attached the files
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 03-22-2015, 08:48 AM by CroNiX.)

Sure, the pagination library is agnostic and can be used for anything that needs pagination, whether it's coming from the db, a xml feed, files on your server, images, whatever. It doesn't care what kind of data it is. All it's doing is creating links to pages based on:
1) the max number of entities you are trying to paginate
2) how many "per page" you set to view
3) what the current page number you are on is.

I haven't looked at your code, and I won't give a complete answer as I have no idea what your actual data looks like, but let's say your $data['controller_files'] array looked like this:

PHP Code:
$data['controller_files'] = array(
  
'item 1',
  
'item 2',
  
'item 3',
  
'item 4',
  
'item 5',
  
'item 6'
); 

And you wanted your pagination to display 2 items per page. You could just use array_chunk().
PHP Code:
$data['controller_files'] = array_chunk($data['controller_files'], 2); //2 for how many per page to display 

Now your array will look like:
PHP Code:
array(
  
=> array(
    
'item 1',
    
'item 2'
  
),
  
=> array(
    
'item 3',
    
'item 4'
  
),
  
=> array(
    
'item 5',
    
'item 6'
  
)
); 

As you can see it broke it up into sub arrays with each containing 2 entries from the original array (the number per/page you want to display), and we have 3 total pages (3 arrays within the main array). To get total number of pages that you'd use in the pagination config, you'd just use the value from count($data['controller_files']), which will return 3.

Now if you want to display page 1, you'd just send to the view:
PHP Code:
$data['controller_files'][0]; //which is item 1, item 2 

If you wanted to display page 3, you'd send:
PHP Code:
$data['controller_files'][2]; //which is item 5, item 6 

Hope that gets you on the right track.
Reply
#3

(This post was last modified: 03-23-2015, 07:10 AM by wolfgang1983.)

(03-22-2015, 08:43 AM)CroNiX Wrote: Sure, the pagination library is agnostic and can be used for anything that needs pagination, whether it's coming from the db, a xml feed, files on your server, images, whatever.  It doesn't care what kind of data it is. All it's doing is creating links to pages based on:
1) the max number of entities you are trying to paginate
2) how many "per page" you set to view
3) what the current page number you are on is.

I haven't looked at your code, and I won't give a complete answer as I have no idea what your actual data looks like, but let's say your $data['controller_files'] array looked like this:




PHP Code:
$data['controller_files'] = array(
 
 'item 1',
 
 'item 2',
 
 'item 3',
 
 'item 4',
 
 'item 5',
 
 'item 6'
); 

And you wanted your pagination to display 2 items per page. You could just use array_chunk().



PHP Code:
$data['controller_files'] = array_chunk($data['controller_files'], 2); //2 for how many per page to display 

Now your array will look like:



PHP Code:
array(
 
 0 => array(
 
   'item 1',
 
   'item 2'
 
 ),
 
 1 => array(
 
   'item 3',
 
   'item 4'
 
 ),
 
 2 => array(
 
   'item 5',
 
   'item 6'
 
 )
); 

As you can see it broke it up into sub arrays with each containing 2 entries from the original array (the number per/page you want to display), and we have 3 total pages (3 arrays within the main array). To get total number of pages that you'd use in the pagination config, you'd just use the value from count($data['controller_files']), which will return 3.

Now if you want to display page 1, you'd just send to the view:



PHP Code:
$data['controller_files'][0]; //which is item 1, item 2 

If you wanted to display page 3, you'd send:



PHP Code:
$data['controller_files'][2]; //which is item 5, item 6 

Hope that gets you on the right track.

You mean some thing like this Working



PHP Code:
<?php

class Extension_permissions extends Admin_Controller {

    public function 
index($page 0) {

 
       $data['title'] = "Permissions";


        $controller_files $this->model_extension_permissions->get_installed_permissions('name'); 

        $data['controller_files'] = array();

        $files glob(FCPATH 'application/modules/admin/controllers/*/*.php') ;

        $per_page 2;

        $offset = ($page 1) * $per_page;

        $paginatedFiles = array();

        $not_count = array(
            FCPATH 'application/modules/admin/controllers/common/dashboard.php',
            FCPATH 'application/modules/admin/controllers/common/login.php',
            FCPATH 'application/modules/admin/controllers/common/logout.php',
            FCPATH 'application/modules/admin/controllers/common/header.php',
            FCPATH 'application/modules/admin/controllers/common/footer.php',
            FCPATH 'application/modules/admin/controllers/common/menu.php',
            FCPATH 'application/modules/admin/controllers/common/register.php',
            FCPATH 'application/modules/admin/controllers/dashboard/customer_total.php',
            FCPATH 'application/modules/admin/controllers/dashboard/user_total.php',
            FCPATH 'application/modules/admin/controllers/dashboard/online.php',
            FCPATH 'application/modules/admin/controllers/error/permission.php',
            FCPATH 'application/modules/admin/controllers/tes/customer_total.php'

        );

        $counted_files array_diff($files$not_count) ;

        if (count($counted_files)) {
            $paginatedFiles array_slice($counted_files$offset$per_pagetrue);
        }

        if ($paginatedFiles) {

            foreach ($paginatedFiles as $file) {

                $controller =  basename(strtolower($file), '.php');

                $modules $this->model_extension_permissions->get_permissions_by_controller($controller);

                $module_data = array();

                foreach ($modules as $module) {
                    $module_data[] = array(
                        'permission_id' => $module['permission_id'],
                        'name' => ucwords(str_replace('_'' '$controller) . ' &gt; ' $module['name']),
                        'edit' => site_url('admin/extension/permissions/update' .'/'$controller .'/'$module['permission_id']),
                        'delete' => site_url('admin/extension/permissions/uninstall' .'/'$controller .'/'$module['permission_id'])
                    );
                }

                $data['controller_files'][] = array(
                    'name' => ucwords(str_replace('_'' ',$controller)),
                    'controller' => $controller,
                    'module'    => $module_data,
                    'install' => site_url('admin/extension/permissions/install') .'/'.$controller,
                    'installed' => in_array($controller$controller_files)
                );
            }
        }

        $this->load->library('pagination');

        $config['base_url'] = base_url()."admin/extension/permissions";
        $config['total_rows'] = count($counted_files);
        $config['per_page'] = $per_page;
        $config['num_links'] = 200;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['full_tag_open'] = '<ul class="pagination pagination-sm">'
        $config['full_tag_close'] = '</ul>'
        $config['num_tag_open'] = '<li>'
        $config['num_tag_close'] = '</li>'
        $config['cur_tag_open'] = '<li class="active"><span>'
        $config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>'
        $config['prev_tag_open'] = '<li>'
        $config['prev_tag_close'] = '</li>'
        $config['next_tag_open'] = '<li>'
        $config['next_tag_close'] = '</li>'
        $config['first_link'] = '&laquo;'
        $config['prev_link'] = '&lsaquo;'
        $config['last_link'] = '&raquo;'
        $config['next_link'] = '&rsaquo;'
        $config['first_tag_open'] = '<li>'
        $config['first_tag_close'] = '</li>'
        $config['last_tag_open'] = '<li>'
        $config['last_tag_close'] = '</li>';

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

        $data['pagination'] = $this->pagination->create_links();

        $this->parser->parse('template/extension/extension_permissions'$data);
    }
 } 
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB