如何让CI分页与array()一起使用;

时间:2015-03-21 08:58:39

标签: codeigniter

如何让codeigniter分页与我的$data['controller_files'][] = array()

一起使用

我无法找出放置uri->段(4)和每页限制的最佳位置。

分页链接有效,但不是每页。仍然显示我桌面上的完整文件列表。

路线

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

控制器

<?php

class Extension_permissions extends Admin_Controller {
    public function index($offset = 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') ;

    if ($files) {

        foreach ($files as $file) {

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

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

            $limit = 1;
            $total_rows = 13; // Number of Current files.

            $config['base_url'] = base_url()."admin/extension/permissions";
            $config['total_rows'] = $total_rows;
            $config['per_page'] = $limit;
            $config['num_links'] = 5;
            $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);     

            $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' => $controller,
                'controller' => $controller,
                'module'    => $module_data,
                'install' => site_url('admin/extension/permissions/install') .'/'.$controller,
                'installed' => in_array($controller, $controller_files)
            );
        }
    }

    $this->parser->parse('template/extension/extension_permissions', $data);
}
}

查看

<div class="panel panel-default">
<div class="panel-heading"><h1 class="panel-title"></h1>{title}</div>
<div class="panel-body">
<div class="table-responsive">

<table class="table table-striped table-bordered table-hover" >
<thead>
    <tr>
    <td data-field="name">Controller Name</td>
    <td class="text-right">Action</td>
    </tr>
</thead id="myTable">
<tbody>
<?php if ($controller_files) { ?>
<?php foreach ($controller_files as $controllers) { ?>

<tr>
<td><?php echo $controllers['name']; ?></td>
<td class="text-right">
<?php if (!$controllers['installed']) { ?>
<a href="<?php echo $controllers['install']; ?>" class="btn btn-success"><i class="fa fa-plus-circle"></i> Install</a>
<?php } else { ?>
<a href="<?php echo $controllers['uninstall']; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i> Uninstall</a>
<?php } ?>
</td>
</tr>

<?php foreach ($controllers['module'] as $module) { ?>
<tr>
<td class="text-left"><?php echo $module['name']; ?></td>
<td class="text-right">
<a href="<?php echo $module['delete']; ?>"  class="btn btn-danger"><i class="fa fa-trash-o"></i> Uninstall</a> 
<a href="<?php echo $module['edit']; ?>"  class="btn btn-primary"><i class="fa fa-pencil"></i> Edit</a>
</td>
</tr>
<?php } ?>

<?php } ?>
<?php } else { ?>
<tr>
    <td class="text-center">No Results</td>
</tr>
<?php } ?>
</tbody>
</table>
</div><!-- Table Responsive End -->
</div><!-- Panel Body End -->
<div class="panel-footer">
    <div class="clearfix">
        <div class="pull-left">
            <?php echo $this->pagination->create_links();?>
        </div>
        <div class="pull-right">
        </div>
    </div>
</div>
</div><!-- Panel Default End -->

3 个答案:

答案 0 :(得分:3)

您可以使用array_slice来获取每页数据。

public function index($page = 1) {

        $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 = 5;

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

        $paginatedFiles = array();

        if (count($files)) {
            $paginatedFiles = array_slice($files, $offset, $per_page, true);
        }

        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' => $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($files);
        $config['per_page'] = $per_page;
        $config['num_links'] = 5;
        $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); 

        $this->parser->parse('template/extension/extension_permissions', $data);
    }

希望它对你有用。

答案 1 :(得分:1)

pagination库是自助服务的,这意味着它没有指示数据的位置/内容,它基本上运行了自己的配置。

您可以使用array_chunk将文件数组拆分为per_page或limits并创建一个新数组,生成索引作为可用于查找偏移量的键

大部分内容可以/应该转移到模型

public function index($offset=0)
{
    $chunks = array();

    // How many files to show per page
    $limit = 5; 

    // an index you can use to find offsets for pagination
    $index=0;

    $count = count($files);

    // loop through the files array in chunks, and create
    // an index or offset you can query from 
    foreach(array_chunk($files, $limit) as $file){
        $index++;
        $chunks[$index] = $file; // build your array 
    }

    $data = $chunks[$offset]; 

    $config = array(
        // pagination config....
    );

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

    $links = $this->pagination->create_links();

    return $this->parser->parse('template/extension/extension_permissions', array(
        'data'   =>  $data,
        'links'  =>  $links
    ));

}

答案 2 :(得分:0)

public function auswertung_ma($per_page = 0)
{
    //PAGINATION
    $this->load->library('pagination');

    $config = $this->config->item('pagination');
    $config['total_rows'] = count($ma_zeiten);
    $config['base_url'] = site_url('site/auswertung_ma');

    $this->pagination->initialize($config);
    $this->data['pagination_links'] = $this->pagination->create_links();
    $this->data['zeiten'] = array_chunk($ma_zeiten, RECORDS_PER_PAGE)[$per_page / 10];