如何在 codeIgniter 中对文件使用分页
Posted
技术标签:
【中文标题】如何在 codeIgniter 中对文件使用分页【英文标题】:How to use pagination with files in codeIgniter 【发布时间】:2016-05-13 16:44:58 【问题描述】:我有一个文件列表,我设法在表格上显示这些文件。但是,我意识到,过了一会儿,这些文件会挤满一页。有没有办法给它们分页?我已经在另一个页面中进行了分页,但使用了数据库。我正在使用 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;
我附上了一张我得到的图片
【问题讨论】:
我添加了一个带有文件分页的答案,如果你愿意,你可以测试一下 这是什么 $this->session->userdata('nric'); var_dump 结果呢? 【参考方案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>
列出表之后的视图中,然后使用:
<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;?>
希望您可以轻松使用它。祝你好运!
【讨论】:
嗯..我看到这使用了模型中的数据库。我实际上只是在计算目录中的文件,我只想对结果进行分页。到目前为止,结果已经出来了。不知道如何仅对它们进行分页。 很简单,您可以更改代码。这里实际上模型根据每页结果的需要获取数据,所以我每次都使用这种代码风格。你现在可以试试这个,如果你发现任何其他简单的方法,你可以使用它。祝你好运!【参考方案2】:我的图像文件有一个工作分页,但你可以为你的 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';
使用我的代码处理图像 1
使用我的代码处理图像 2
【讨论】:
嗨!感谢回复。我在问题中编辑了我的代码并添加了一张图片。会试试你的方法,看看效果如何。 对不起,我离开了一个星期。会尝试让你知道。顺便说一句,我了解 URI Routes,但是查询字符串是什么意思?【参考方案3】:试试这个代码 在应用程序/配置/文件夹 添加分页.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' => '« Previous',
'prev_tag_open' => '<li >',
'prev_tag_close' => '</li>',
'next_link' => 'Next »',
'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 »',
'last_tag_open' => '</a><li>',
'last_tag_close' => '</li>',
'first_link' => '« 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】:我假设你在 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 限制 = 10
你必须得到文件从 20 到 30,if count >= (30-10) && count < 30
,然后你得到 20,21,22...29
所以 10 个文件!
如果是 offset = 10,limit = 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()
【讨论】:
【参考方案5】:试试这个代码
$_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();
【讨论】:
【参考方案6】:在控制器中使用此代码
$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
【讨论】:
以上是关于如何在 codeIgniter 中对文件使用分页的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Codeigniter 分页中使用页码而不是偏移量?