如何在codeIgniter中使用分页文件

时间:2016-02-04 07:18:42

标签: php codeigniter pagination

我有一个我已设法在表格上显示的文件列表。但是,我意识到一段时间后,这些文件会占用一个页面。有没有办法对它们进行分页?我在另一个页面中进行了分页但是使用了数据库。我正在使用Codeigniter。

继承我的观点

<table class="table">
<thead>
<tr>
    <th>NRIC</th>
    <th>Product</th>
    <th>Policy Number</th>
    <th></th>
</tr>
</thead>
<tbody>
<?php foreach($data as $row): ?>
<tr data-url="<?php echo $row['url']; ?>" data-policyno="<?php echo $row['policyno']; ?>">
 <td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['product']; ?></td>
<td><?php echo $row['policyno']; ?></td>
<td><input type="button" class="retrievedoc" value="View"/></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

我的控制器

function index()
{   

    $this->load->helper('directory');
    $this->load->library('pagination');
    $this->load->model('statement');
    $map = directory_map('./assets/data/');
    $nric = $this->session->userdata('nric');
    $count=0;

    $config = array();
    $config['base_url']=site_url('user/statements/index');
    $config['total_rows']=$count;
    $config['per_page']=5;
    $this->pagination->initialize($config);

    $data['data']['result'] = $this->statement->retrieve($nric);
    $data['data']['links'] = $this->pagination->create_links(); 

    $this->load->view('includes/user/header');
    $this->load->view('user/statements',$data);
    $this->load->view('includes/user/footer');
}

模型

public function retrieve($nric)
{
    $count=0;
    $map = directory_map('./assets/data/');
    foreach($map as $row)
    {
         $separate = explode('_',trim($row,".pdf"));
         if($nric == $separate[0])
         {
            $count++;
            $data['data'][] = array(
             'firstname' => $separate[0],
             'product' => $separate[1],
             'policyno' => $separate[2],
             'url'=> base_url().'assets/data/'.$row
             );
         }
    }
    return $data;
}

我附上了我正在获得的图像

enter image description here

6 个答案:

答案 0 :(得分:1)

在Controller中获取列表:

function all_deal($pagi='10',$sortField='id',$order='DESC',$start='0')
{
    $filters = array();
    $start = $this->uri->segment(6);
    $data = $this->deal_model->get_all_deal($filters, $sortField=$sortField, $order, $start,$pagi);
    $page =($this->uri->segment(6))? $this->uri->segment(6):0;

    $config                      = array();
    $config["base_url"]          = base_url() ."index.php/deals/all_deal/".$pagi.'/'.$sortField.'/'.$order;
    $config["total_rows"]        = $data['count'];
    $config["per_page"]          = $pagi;
    $config["uri_segment"]       = 6;
    $config['use_page_numbers']  = false;
    $config['next_link']         = 'Next';
    $config['prev_link']         = 'Previous';

    $this->pagination->initialize($config);
    $paginglink =$this->pagination->create_links(); 

    $this->load->view('deal_list',array('data'=>$data['data'],'paginglink'=>$paginglink,'pagi'=>$pagi,'sortField'=>$sortField,'order'=>$order,'start'=>$start,'totalcount'=>$data['count']));
}

在模特:

function get_all_deal($filters,$sortField, $order, $start,$limit)
 {
    if($limit==''){
            $pagi='10';
        }
        $limit = (int) $limit;
        $start = (int) $start;
        $date1=date('Y-m-d');
        $this->db->select("SQL_CALC_FOUND_ROWS deals.*", false)
                ->from('deals');
        $this->db->limit($limit, $start);

        $this->db->order_by($sortField.' '.$order);
        $data['data'] = $this->db->get()->result_array();
        $last_query =  $this->db->last_query();
        $data['count'] = $this->db->query('SELECT FOUND_ROWS() as num_rows')->first_row()->num_rows;

        return $data;
 }

在最后一次创建新表格行或<tr>及其中<td>的列表表后面的View中,然后使用:

<span style="color:#000">Showing</span>
                    <?php 
                    echo $start+1; 
                    ?><span style="color:#000"> to</span>
                    <?php
                     $uri=$this->uri->segment(6);
                     if($uri)
                     {
                        $records=$uri+$pagi;
                        if($totalcount<$records)
                        {
                            echo $totalcount;
                        }
                        else
                        {
                            echo $uri+$pagi;
                        }

                     }
                       else
                        {
                            echo $pagi;
                        }
                    ?> <span style="color:#000">of</span>
                    <?php echo $totalcount; ?>
                    </div>
                    <?php echo $paginglink;?>

希望你能轻松使用它。祝你好运!

答案 1 :(得分:1)

我对我的图像文件有一个工作分页,但你可以为你的pdf文件做同样的事情。目前我已经使用查询字符串进行分页。但如果您愿意,可以将其转换为URI Segments

如果您不打算使用查询字符串

,则可能需要设置URI Routes

注意:您可能需要设置一些路线

随意尝试。

<?php

class Filemanager extends CI_Controller {

    public function index() {
        $directory = scandir(FCPATH . 'assets/data', 1);

        $files = array_diff($directory, array('.', '..'));
        $files_count = count($files);

        // Set your displayed limit.
        $files_limit = 2;

        // The input get you could rename to a uri segment if not using query string
        $input_get_per_page = $this->input->get('per_page');
        $input_get_per_page += $files_limit;

        foreach($files as $file => $value) {

            if ($file < $input_get_per_page && $file >= $input_get_per_page - $files_limit) {

                var_dump($value);

                // Your code here use 
                // Use $value to get filename.

            }

        }

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

        $config['base_url'] = base_url('index.php?d=common&c=filemanager');
        $config['total_rows'] = $files_count;
        $config['per_page'] = $files_limit;
        $config['page_query_string'] = TRUE;
        $config['num_links'] = "16";
        $config['full_tag_open'] = "<ul class='pagination'>";
        $config['full_tag_close'] = "</ul>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
        $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
        $config['next_tag_open'] = "<li>";
        $config['next_tagl_close'] = "</li>";
        $config['prev_tag_open'] = "<li>";
        $config['prev_tagl_close'] = "</li>";
        $config['first_tag_open'] = "<li>";
        $config['first_tagl_close'] = "</li>";
        $config['last_tag_open'] = "<li>";
        $config['last_tagl_close'] = "</li>";

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

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

        $this->load->view('filemanager_view', $data);
    }
}

我的配置如何。

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'REQUEST_URI' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'REQUEST_URI'    Uses $_SERVER['REQUEST_URI']
| 'QUERY_STRING'   Uses $_SERVER['QUERY_STRING']
| 'PATH_INFO'      Uses $_SERVER['PATH_INFO']
|
| WARNING: If you set this to 'PATH_INFO', URIs will always be URL-decoded!
*/
$config['uri_protocol'] = 'QUERY_STRING';

/*
|--------------------------------------------------------------------------
| Enable Query Strings
|--------------------------------------------------------------------------
|
| By default CodeIgniter uses search-engine friendly segment based URLs:
| example.com/who/what/where/
|
| By default CodeIgniter enables access to the $_GET array.  If for some
| reason you would like to disable it, set 'allow_get_array' to FALSE.
|
| You can optionally enable standard query string based URLs:
| example.com?who=me&what=something&where=here
|
| Options are: TRUE or FALSE (boolean)
|
| The other items let you set the query string 'words' that will
| invoke your controllers and its functions:
| example.com/index.php?c=controller&m=function
|
| Please note that some of the helpers won't work as expected when
| this feature is enabled, since CodeIgniter is designed primarily to
| use segment based URLs.
|
*/
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';

使用我的代码工作图像

enter image description here

使用我的代码工作图像2

enter image description here

答案 2 :(得分:0)

试试此代码

    $_total_rows=100; # call here dynamic total rows
    $url = base_url().'call-here-your-url'; # call here your url
    $per_page = 20; # change it from yours

    $get_url='&field1=value&field2=value'; # use here your parameters 


    $this->load->library('pagination'); 
    $config['base_url'] =$url.'?p=0'.$get_url;
    $config['total_rows'] = $_total_rows; 
    $config['per_page'] = $per_page;
    $config['page_query_string'] = TRUE;

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

答案 3 :(得分:0)

试试这段代码 在application / config /文件夹中 添加paging.php

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
    'paging' => array(
        'full_tag_open'     => '<ul>', 
        'full_tag_close'    => '</ul>', 
        'prev_link'         => '&laquo; Previous', 
        'prev_tag_open'     => '<li >', 
        'prev_tag_close'    => '</li>', 
        'next_link'         => 'Next &raquo;', 
        'next_tag_open'     => '<li >', 
        'next_tag_close'    => '</li>', 
        'cur_tag_open'      => '<li><a class="active">', 
        'cur_tag_close'     => '</li>', 
        'num_tag_open'      => '<li>', 
        'num_tag_close'     => '</li>', 
        'last_link'         => 'Last &raquo;', 
        'last_tag_open'     => '</a><li>', 
        'last_tag_close'    => '</li>', 
        'first_link'        => '&laquo; First', 
        'first_tag_open'    => '<li >', 
        'first_tag_close'   => '</li>'
    )
);

在Controller函数中添加以下代码

$paging_config = $this->config->item('paging');
    $paging_config['uri_segment'] = 2;//uri segment 
    $paging_config['per_page'] = 5;//per page 
    $paging_config['base_url'] = site_url();//full of pagination function
    $this->pagination->initialize($paging_config);

在视图中添加分页

<?php echo $this->pagination->create_links();?>

答案 4 :(得分:0)

在Controller中使用此代码

$this->load->library("pagination");
$config["base_url"] = base_url() . "controller/function_name";
$config["total_rows"] = $total->num_rows();
$config["per_page"] = 1000;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = 3;
$this->pagination->initialize($config);

//在视图页面中 <p><?php echo $links1; ?></p> try this one

答案 5 :(得分:0)

我认为你在CI分页方面有足够的经验,但是因为你处理文件而不是db,可能会让你感到困惑。

要处理创建带参数(偏移,限制)的函数所需的文件,并返回一个包含总行数和数据的数组。

function getFiles($offset, $limit) {
 $count = 0;
 $start = $offset - $limit;
 $result = array();

 // get files from directory

 // foreach files and fill array like

 if ($count >= $start  && $count < $offset) {
 $result['data'][] = array(
         'firstname' => $separate[0],
         'product' => $separate[1],
         'policyno' => $separate[2],
         'url'=> base_url().'assets/data/'.$row
  );
 }
 // increase count
 $count++;

 // end foreach

 $result['total_rows'] = $count;

 return $result;
}

使用上述函数,您将获得文件的总数,并且还有一些文件的数组取决于您设置的偏移量和限制。

在foreach内部的if中,根据偏移,限制和比较计数,然后用正确的文件填充数组。

示例:

偏移= 30 limit = 10

您必须获取20到30之间的文件if count >= (30-10) && count < 30,然后获得20,21,22 ... 29

所以10个文件!

如果偏移= 10,则限制= 10,然后是0,1,2 ... 9

再次发送10个文件!

最后你设置了你知道的分页和vuala!

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

$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = $result['total_rows']; 
$config['per_page'] = 10; // your limit

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

echo $this->pagination->create_links()