需要 MVC 的分页脚本



我有自定义MVC并坚持分页。请给我代码,我的MVC结构在这里。 索引>控制器>模型>返回控制器>然后视图。

请帮助我创建分页以适应该 MVC。

我的控制器:

class Posts extends Controller{

public function __construct(){
$this->postModel = $this->model('Post');
}
public function index(){
$allpost         =       $this->postModel->getAllPosts();
$data = [
'posts'       =>      $allpost,
];
$this->view('jobs/index', $data);}}

这是我的模型:

class Post {
private  $db;
public function __construct(){
$this->db = new Database;
}
public function getAllPosts(){
$this->db->query('select * from posts where active = 0 ');
return $this->db->resultSet();
}

这是我的观点:

<?php foreach($data['allposts'] as $posts) : ?>
<div class="jobContent">
<div class="jobTitle">
<h5><a href="="><?= $posts->title; ?></a></h5>
</div>
<div class="cName">
<a href=""><?= $posts->des; ?></a>
</div>
</div>
<?php endforeach ;?>

使用数据表,它为您提供分页和搜索功能。

例如,您的表类似于<table id='sth'> </table>,只需添加以下 CDN:

<link href="https://cdn.datatables.net/1.10.18/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css">

也:<script src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js" type="text/javascript"></script>

<script src="https://cdn.datatables.net/1.10.18/js/dataTables.bootstrap.min.js" type="text/javascript"></script>

然后添加以下内容:

<script>
$(document).ready(function () {
$('#sth').DataTable();
});
</script>
<?php
class Pagination{
protected $baseURL        = '';
protected $totalRows      = '';
protected $perPage        = 10;
protected $numLinks       = 2;
protected $currentPage    =  0;
protected $firstLink      = 'First';
protected $nextLink       = 'Next &raquo;';
protected $prevLink       = '&laquo; Prev';
protected $lastLink       = 'Last';
protected $fullTagOpen    = '<div class="pagination">';
protected $fullTagClose   = '</div>';
protected $firstTagOpen   = '';
protected $firstTagClose  = '&nbsp;';
protected $lastTagOpen    = '&nbsp;';
protected $lastTagClose    = '';
protected $curTagOpen    = '&nbsp;<b>';
protected $curTagClose    = '</b>';
protected $nextTagOpen    = '&nbsp;';
protected $nextTagClose    = '&nbsp;';
protected $prevTagOpen    = '&nbsp;';
protected $prevTagClose    = '';
protected $numTagOpen    = '&nbsp;';
protected $numTagClose    = '';
protected $showCount    = true;
protected $currentOffset= 0;
protected $queryStringSegment = 'page';
function __construct($params = array()){
if (count($params) > 0){
$this->initialize($params);        
}
}
function initialize($params = array()){
if (count($params) > 0){
foreach ($params as $key => $val){
if (isset($this->$key)){
$this->$key = $val;
}
}        
}
}
/**
* Generate the pagination links
*/    
function createLinks(){ 
// If total number of rows is zero, do not need to continue
if ($this->totalRows == 0 OR $this->perPage == 0){
return '';
}
// Calculate the total number of pages
$numPages = ceil($this->totalRows / $this->perPage);
// Is there only one page? will not need to continue
if ($numPages == 1){
if ($this->showCount){
$info = 'Showing : ' . $this->totalRows;
return $info;
}else{
return '';
}
}
// Determine query string
$query_string_sep = (strpos($this->baseURL, '?') === FALSE) ? '?page=' : '&amp;page=';
$this->baseURL = $this->baseURL.$query_string_sep;
// Determine the current page
$this->currentPage = isset($_GET[$this->queryStringSegment])?$_GET[$this->queryStringSegment]:0;
if (!is_numeric($this->currentPage) || $this->currentPage == 0){
$this->currentPage = 1;
}
// Links content string variable
$output = '';
// Showing links notification
if ($this->showCount){
$currentOffset = ($this->currentPage > 1)?($this->currentPage - 1)*$this->perPage:$this->currentPage;
$info = 'Showing ' . $currentOffset . ' to ' ;
if( ($currentOffset + $this->perPage) <= $this->totalRows )
$info .= $this->currentPage * $this->perPage;
else
$info .= $this->totalRows;
$info .= ' of ' . $this->totalRows . ' | ';
$output .= $info;
}
$this->numLinks = (int)$this->numLinks;
// Is the page number beyond the result range? the last page will show
if($this->currentPage > $this->totalRows){
$this->currentPage = $numPages;
}
$uriPageNum = $this->currentPage;
// Calculate the start and end numbers. 
$start = (($this->currentPage - $this->numLinks) > 0) ? $this->currentPage - ($this->numLinks - 1) : 1;
$end   = (($this->currentPage + $this->numLinks) < $numPages) ? $this->currentPage + $this->numLinks : $numPages;
// Render the "First" link
if($this->currentPage > $this->numLinks){
$firstPageURL = str_replace($query_string_sep,'',$this->baseURL);
$output .= $this->firstTagOpen.'<a href="'.$firstPageURL.'">'.$this->firstLink.'</a>'.$this->firstTagClose;
}
// Render the "previous" link
if($this->currentPage != 1){
$i = ($uriPageNum - 1);
if($i == 0) $i = '';
$output .= $this->prevTagOpen.'<a href="'.$this->baseURL.$i.'">'.$this->prevLink.'</a>'.$this->prevTagClose;
}
// Write the digit links
for($loop = $start -1; $loop <= $end; $loop++){
$i = $loop;
if($i >= 1){
if($this->currentPage == $loop){
$output .= $this->curTagOpen.$loop.$this->curTagClose;
}else{
$output .= $this->numTagOpen.'<a href="'.$this->baseURL.$i.'">'.$loop.'</a>'.$this->numTagClose;
}
}
}
// Render the "next" link
if($this->currentPage < $numPages){
$i = ($this->currentPage + 1);
$output .= $this->nextTagOpen.'<a href="'.$this->baseURL.$i.'">'.$this->nextLink.'</a>'.$this->nextTagClose;
}
// Render the "Last" link
if(($this->currentPage + $this->numLinks) < $numPages){
$i = $numPages;
$output .= $this->lastTagOpen.'<a href="'.$this->baseURL.$i.'">'.$this->lastLink.'</a>'.$this->lastTagClose;
}
// Remove double slashes
$output = preg_replace("#([^:])//+#", "\1/", $output);
// Add the wrapper HTML if exists
$output = $this->fullTagOpen.$output.$this->fullTagClose;
return $output;        
}

}

这里代码索引:

$baseURL = 'http://example.com/php_pagination/index.php';
$limit = 5;
$offset = !empty($_GET['page'])?(($_GET['page']-1)*$limit):0;
foreach($$data['allcount'] as $allcount){
$rowCount = $allcount->count;
}
$pagConfig = array(
'baseURL' => $baseURL,
'totalRows'=>$rowCount,
'perPage'=>$limit
);
$pagination =  new Pagination($pagConfig);
<div class="post-list">
<?php foreach($data['alljobs'] as $alljobs){
<?php echo $alljobs->job_title; ?>
} ?>
<div class="list-item">
<a href="javascript:void(0);"><?php echo $row["title"]; ?></a>
</div>
<?php } ?>
</div>
<?php echo $pagination->createLinks(); ?>
<?php } ?>enter code here

通过使用该代码,我可以获取分页,但记录不会更改其在所有页面中的显示相同。

最新更新