donglongqiao9595 2015-03-23 12:27
浏览 19
已采纳

array_diff无法使用count codeigniter

When I use my pagination I do not want it to count certain files in my array. I have tried array diff but not working.

How can I make it so that does not count any thing that is in my $not_count array() and so my $config['total_rows'] = $counted_files; show correct count.

<?php

class Extension_permissions extends Admin_Controller {

public function __construct() {
    parent::__construct();
    $this->load->model('admin/extension/model_extension_permissions');
    if ($this->model_extension_permissions->check_permission_access() == true) {
        return true;
    } else {
        redirect('admin/permission');
    }
}

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 = 1;

    $offset = $page * $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'
    );

    $counted_files = array_diff($not_count, count($files)) ;

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

    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'] = $counted_files;
    $config['per_page'] = $per_page;
    $config['num_links'] = 200;
    $config['uri_segment'] = 4;
    $config['use_page_numbers'] = TRUE;

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

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

1条回答 默认 最新

  • drhib62644 2015-03-23 13:37
    关注

    First parameter of array_diff is the array to compare from and second parameter is array to compare against. In this case, you want to exclude $not_count from $files. Your code should be like that.

    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();
    
            $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'
            );
    
            $counted_files = array_diff($files, $not_count) ;
    
            if (count($counted_files)) {
                $paginatedFiles = array_slice($counted_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($counted_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);
        }
    

    Hope it will be useful for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀