我尝试为此格式的链接创建分页脚本:http://example.com/some-page/1
其中1
将指示当前正在查看的网页。
到目前为止,我已经能够提出一个分页课程,下面是:
<?php
class Pagination {
private $_db,
$_properties = array(
'max' => 5,
'page' => 1,
'total' => 0,
'pages' => 0,
);
public function __construct() {
$this->_db = DB::getInstance();
}
public function getPage($page = 2) {
$this->_properties['page'] = (int)$page;
}
public function pageStart() {
$start = ( $this->getPage() > 1 ) ? ( $this->getPage() * $this->pageLimit() ) : 0;
return $start;
}
public function pageLimit($max = 5) {
$this->_properties['max'] = (int)$max;
}
public function getData($query) {
$data = $this->_db->query($query . "LIMIT {$this->pageStart()}, {$this->pageLimit()}");
if ( $data->count() ) {
$this->_properties['total'] = $data->count();
return $data->results();
}
return false;
}
public function totalCount() {
return $this->_properties['total'];
}
public function currentPage() {
return $this->_properties['page'];
}
public function renderLinks() {
$total = $this->totalCount();
$perPage = $this->pageLimit();
$pages = ceil($total / $perPage);
$currentPage = $this->currentPage();
$output = "<div class=\"col-md-12 col-xs-12 col-sm-12\">";
$output .= "<ul class=\"pagination pagination-lg\">";
$output .= "<li> <a href=\"". $_SERVER['REQUEST_URI'] . '/' . ($currentPage - 1) ."\"> <i class=\"fa fa-chevron-left\" aria-hidden=\"true\"></i></a></li>";
for ($i = 1; $i <= $pages ; $i++) {
$status = ($currentPage == $i) ? " class=\"active\"" : "";
$output .= "<li". $status ." > <a href=\"/". $i ."\">". $i ."</a> </li>";
}
$output .= "<li><a href=\"/". $_SERVER['REQUEST_URI'] . '/' . $i ."\"> <i class=\"fa fa-chevron-right\" aria-hidden=\"true\"></i></a></li>";
$output .= "<ul>";
$output .= "</div>";
return $output;
}
}
我在需要它的页面中调用它:
$userServices = new Pagination();
$userServices->getPage($thirdvar);
$userServices->pageLimit(2);
$servicesData = $userServices->getData("SELECT * FROM `table` WHERE `id` = 4");
然后我做一个foreach循环来获取$serviceData
的内容,如下所示:
foreach ( $serviceData as $service ) {
// output content here
}
然后是导航链接:
<?php echo $userServices->renderLinks(); ?>
当我运行该页面时,出现错误:Warning: Division by zero in C:\xampp7\htdocs\directory\model\Pagination.php on line 49
。也就是说,我有$pages = ceil($total / $perPage);
但也有错误:Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011744 bytes) in C:\xampp7\htdocs\directory\model\Pagination.php on line 58
,我有$output .= "<li". $status ." > <a href=\"/". $i ."\">". $i ."</a> </li>";
。
到目前为止,当我手动输入一个数字来替换$pages = ceil($total / 4);
时,错误消失了,但我没有得到页面中的数据。
我在哪里做错了?
修改
因此,在进行了一些更正(基于答案和评论)后,我发现没有填充数据的原因来自这一行:$data = $this->_db->query($query . "LIMIT $this->pageStart(), $this->pageLimit()");
。对于 who-know-why ,我不知道为什么$this->pageStart()
和$this->pageLimit()
没有返回任何值,从而使查询失败。
有人可以指出我正确的道路吗?
答案 0 :(得分:2)
$perPage = $this->pageLimit();
然而pageLimit()
函数没有返回任何内容。这就是问题所在。
答案 1 :(得分:0)
这应该可以解决第二个错误
<form action="...." method="post" enctype="multipart/form-data">
.
.
.
.
<div class="form-group{{ $errors->has('img.'.$i) ? ' has-error' : '' }}">
<label for="img">File input</label>
<input type="file" class="form-control-file" id="img" value="{{old('img.'.$i)}}" name="img[]">
<small class="text-danger">{{ $errors->first('img.'.$i) }}</small>
</div>
而不是16M传递高度值(我为你留下了数学)。但这不是一个好的做法,可以让你的代码更有效率。
在49行之前说这个
ini_set('memory_limit','16M');
并输出输出。所以我可以帮助你。
编辑:
您需要处理许多问题。删除die();
答案 2 :(得分:0)
所以经过这么多的调试,以及从一些评论中给出的见解,我能够找出我的分页课的问题。
首先,我将getData()
方法更新为:
public function getData($query) {
$data = $this->_db->query("{$query} LIMIT {$this->pageStart()}, {$this->pageLimit()}");
if ( $data->count() ) {
return $data->results();
}
$tQuery = $this->_db->query("SELECT FOUND_ROWS() AS total");
$this->_properties['total'] = $tQuery->first()->total;
}
然后使用以下命令在我的页面中调用它:
$servicesData = $userServices->getData("SELECT SQL_CALC_FOUND_ROWS * FROM `table` WHERE `id` = 1");
此外,我添加了一些新方法renderPage()
,renderLimit()
,baseUrl()
和renderUrl()
,这些方法将返回getPage()
,pageLimit()
的值,获取将要输入的url结构,并分别返回baseUrl()
的值。
然后我也更新了renderLinks()
方法,如下所示:
public function renderLinks() {
$total = $this->totalCount();
$perPage = $this->renderLimit();
$pages = ceil($total / $perPage);
$currentPage = $this->renderPage();
$prevCalc = $currentPage - 1;
$prevStat = ($prevCalc == 0) ? "" : " href=\"". $this->renderUrl() . $prevCalc ."\"";
$nextCalc = $currentPage + 1;
$nextStat = ($nextCalc > $pages) ? "" : " href=\"". $this->renderUrl() . $nextCalc ."\"";
$output = "<div class=\"col-md-12 col-xs-12 col-sm-12\">";
$output .= "<ul class=\"pagination pagination-lg\">";
$output .= "<li> <a". $prevStat ."> <i class=\"fa fa-chevron-left\" aria-hidden=\"true\"></i></a></li>";
for ($i = 1; $i <= $pages ; $i++) {
$status = ($currentPage == $i) ? " class=\"active\"" : "";
$output .= "<li". $status ." > <a href=\"". $this->renderUrl() . $i ."\">". $i ."</a> </li>";
}
$output .= "<li><a". $nextStat ."> <i class=\"fa fa-chevron-right\" aria-hidden=\"true\"></i></a></li>";
$output .= "<ul>";
$output .= "</div>";
return $output;
}
总而言之,我的分页类看起来像这样:
<?php
class Pagination {
private $_db,
$_properties = array(
'max' => 5,
'page' => 1,
'total' => 4,
'base_url' => '',
);
public function __construct() {
$this->_db = DB::getInstance();
}
public function getPage($page = null) {
return( $page && $page != "" ) ? $this->_properties['page'] = (int)$page : $this->_properties['page'];
}
public function renderPage() {
return $this->_properties['page'];
}
public function pageLimit($max = null) {
return( $max ) ? $this->_properties['max'] = (int)$max : $this->_properties['max'];
}
public function renderLimit() {
return $this->_properties['max'];
}
public function pageStart() {
return( $this->renderPage() > 1 ) ? ($this->renderPage() * $this->renderLimit()) - $this->pageLimit() : 0;
}
public function getData($query) {
$data = $this->_db->query("{$query} LIMIT {$this->pageStart()}, {$this->pageLimit()}");
if ( $data->count() ) {
return $data->results();
}
$tQuery = $this->_db->query("SELECT FOUND_ROWS() AS total");
$this->_properties['total'] = $tQuery->first()->total;
}
public function totalCount() {
return $this->_properties['total'];
}
public function baseUrl($url = null) {
return( $url ) ? $this->_properties['base_url'] = escape_url($url) : $this->_properties['base_url'];
}
public function renderUrl() {
return $this->_properties['base_url'];
}
public function renderLinks() {
$total = $this->totalCount();
$perPage = $this->renderLimit();
$pages = ceil($total / $perPage);
$currentPage = $this->renderPage();
$prevCalc = $currentPage - 1;
$prevStat = ($prevCalc == 0) ? "" : " href=\"". $this->renderUrl() . $prevCalc ."\"";
$nextCalc = $currentPage + 1;
$nextStat = ($nextCalc > $pages) ? "" : " href=\"". $this->renderUrl() . $nextCalc ."\"";
$output = "<div class=\"col-md-12 col-xs-12 col-sm-12\">";
$output .= "<ul class=\"pagination pagination-lg\">";
$output .= "<li> <a". $prevStat ."> <i class=\"fa fa-chevron-left\" aria-hidden=\"true\"></i></a></li>";
for ($i = 1; $i <= $pages ; $i++) {
$status = ($currentPage == $i) ? " class=\"active\"" : "";
$output .= "<li". $status ." > <a href=\"". $this->renderUrl() . $i ."\">". $i ."</a> </li>";
}
$output .= "<li><a". $nextStat ."> <i class=\"fa fa-chevron-right\" aria-hidden=\"true\"></i></a></li>";
$output .= "<ul>";
$output .= "</div>";
return $output;
}
}
希望它可以帮助其他人。