CakePHP分页,不工作(未正确显示数据)
当我激活管理路由时,我很难想出如何在cakePHP中分页 我有CakePHP分页,不工作(未正确显示数据),cakephp,pagination,cakephp-2.0,cakephp-2.1,cakephp-2.3,Cakephp,Pagination,Cakephp 2.0,Cakephp 2.1,Cakephp 2.3,当我激活管理路由时,我很难想出如何在cakePHP中分页 我有localhost/myapp这是主页 和localhost/myapp/admin,这是管理区域。所以我想在前端分页,在localhost/myapp索引页面中 所以作为主索引页的是类型学模型的索引。 在我的app/Config/routes.php中,我有一个主页的路由: Router::connect( '/', array( 'admin'=>false, 'controlle
localhost/myapp
这是主页
和localhost/myapp/admin,这是管理区域。所以我想在前端分页,在localhost/myapp索引页面中
所以作为主索引页的是类型学模型的索引。
在我的app/Config/routes.php中,我有一个主页的路由:
Router::connect(
'/',
array(
'admin'=>false,
'controller' => 'typologies',
'action' => 'index', 'index'
)
);
PS:我也使用slug来查找帖子
在我的应用程序/控制器/类型控制器中,我有以下内容:
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'LEFT',
'conditions' => array('Item.id = Typology.item_id')
)
),
'conditions' => array(
'Item.published' => 1,
'Typology.published' => 1
),
'limit' => 1,
'order' => array('Typology.sort' => 'ASC'),
'fields' => array('Item.*', 'Typology.*'),
'recursive' => 2
);
$typologies = $this->paginate('Typology');
$this->set(compact('typologies'));
<?php
$result = '';
foreach ($typologies as $typology):
$id = h($typology['Typology']['id']);
$item_id = h($typology['Typology']['item_id']);
$title = h($typology['Typology']['title']);
$description = h($typology['Typology']['description']);
$thumbnail = h($typology['Typology']['thumbnail']);
$price = h($typology['Typology']['price']);
$typology_category_id = h($typology['Typology']['typology_category_id']);
$typology_condition_id = h($typology['Typology']['typology_condition_id']);
if (strlen($description)>330) {
$short_description = substr($description, 0, 327);
$description = $short_description."...";
}
$dir = "img/uploads/typology/thumbnails/";
if (file_exists( $dir . $thumbnail)) {
$typologyThumbnails = $dir . $thumbnail;
}
else {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
if($thumbnail=='NULL' || $thumbnail=='') {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
$result .= "<div class=\"item_shadow\">";
if ($logged_in) {
if ($typology['Typology']['published']==0 || $typology['Typology']['published']==false || $typology['TypologyItem']['published']==0 || $typology['TypologyItem']['published']==false) {
$result .= "<div class=\"badge red\"> non publicato </div>";
}
}
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
$result .= "<div class=\"item-content\">";
$result .= "<div class=\"item-top-content\">";
$result .= "<div class=\"item-top-content-inner\">";
$result .= "<div class=\"item-top-title\">";
$result .= "<h4>{$title}</h4>";
$result .= "</div>";
$result .= "</div>" ;
$result .= "</div>";
$result .= "<div class=\"item-add-content\">";
$result .= "<div class=\"item-add-content-inner\">";
$result .= "<div class=\"description-inner\">";
$result .= "<p>{$description}</p>";
$result .= "</div>";
$result .= "<div class=\"read-more-inner\">";
$result .= $this->Html->link("maggiori informazioni". $this->Html->image('elenco.png'), array('admin'=>false,'controller' => 'items', 'action' => 'view', 'slug' => Inflector::slug($typology['TypologyItem']['seo_url'],'-'),'id'=>$typology['Typology']['item_id'],'?'=> array('active_tab' => $typology['Typology']['id'])), array('escape' => false));
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
endforeach;
$result .= "";
?>
我将limit=>1设置为测试目的。
在我的视图/类型/索引.ctp中,我有以下内容:
$this->Paginator->settings = array(
'joins' => array(
array(
'table' => 'items',
'alias' => 'Item',
'type' => 'LEFT',
'conditions' => array('Item.id = Typology.item_id')
)
),
'conditions' => array(
'Item.published' => 1,
'Typology.published' => 1
),
'limit' => 1,
'order' => array('Typology.sort' => 'ASC'),
'fields' => array('Item.*', 'Typology.*'),
'recursive' => 2
);
$typologies = $this->paginate('Typology');
$this->set(compact('typologies'));
<?php
$result = '';
foreach ($typologies as $typology):
$id = h($typology['Typology']['id']);
$item_id = h($typology['Typology']['item_id']);
$title = h($typology['Typology']['title']);
$description = h($typology['Typology']['description']);
$thumbnail = h($typology['Typology']['thumbnail']);
$price = h($typology['Typology']['price']);
$typology_category_id = h($typology['Typology']['typology_category_id']);
$typology_condition_id = h($typology['Typology']['typology_condition_id']);
if (strlen($description)>330) {
$short_description = substr($description, 0, 327);
$description = $short_description."...";
}
$dir = "img/uploads/typology/thumbnails/";
if (file_exists( $dir . $thumbnail)) {
$typologyThumbnails = $dir . $thumbnail;
}
else {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
if($thumbnail=='NULL' || $thumbnail=='') {
$typologyThumbnails = "img/uploads/noimg.jpg";
}
$result .= "<div class=\"item_shadow\">";
if ($logged_in) {
if ($typology['Typology']['published']==0 || $typology['Typology']['published']==false || $typology['TypologyItem']['published']==0 || $typology['TypologyItem']['published']==false) {
$result .= "<div class=\"badge red\"> non publicato </div>";
}
}
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
$result .= "<div class=\"item-content\">";
$result .= "<div class=\"item-top-content\">";
$result .= "<div class=\"item-top-content-inner\">";
$result .= "<div class=\"item-top-title\">";
$result .= "<h4>{$title}</h4>";
$result .= "</div>";
$result .= "</div>" ;
$result .= "</div>";
$result .= "<div class=\"item-add-content\">";
$result .= "<div class=\"item-add-content-inner\">";
$result .= "<div class=\"description-inner\">";
$result .= "<p>{$description}</p>";
$result .= "</div>";
$result .= "<div class=\"read-more-inner\">";
$result .= $this->Html->link("maggiori informazioni". $this->Html->image('elenco.png'), array('admin'=>false,'controller' => 'items', 'action' => 'view', 'slug' => Inflector::slug($typology['TypologyItem']['seo_url'],'-'),'id'=>$typology['Typology']['item_id'],'?'=> array('active_tab' => $typology['Typology']['id'])), array('escape' => false));
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
$result .= "</div>";
endforeach;
$result .= "";
?>
如您所见,首先,我将所有数据存储到一个变量中,然后使用paginator助手像这样回显它:
<?php
if (isset($result)) {
echo $result;
}
?>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page{:page} di {:pages}, Showing {:current} records of {:count} in total, starting at {:start}, and finishing at {:end}')
));
?>
</p>
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('Preview'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('Next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>
所以我第一次加载页面时,URL是这样的http://localhost/mycakephpApp/
它显示一个日期,就像它假设的那样:
当我单击next(下一步)或数字2时,URL如下所示:http://localhost/mycakephpApp/typologies/index/index/page:2
,它应该显示下一个。是的,但现在是这样的空白。
我不知道我错过了什么或者我做错了什么。有人知道如何解决这个问题吗?
我非常感谢您的帮助。相对URL有问题
该视图包含以下内容:
$result .= "<div class=\"item\" style=\"background-image:url({$typologyThumbnails});\">";
这意味着对于urlhttp://localhost/mycakephpApp/
请求以下图像url:
http://localhost/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
http://localhost/mycakephpApp/typologies/index/index/img/uploads/typology/thumbnails/whatever.jpg
但是,对于urlhttp://localhost/mycakephpApp/typologies/index/index/page:2
请求以下图像url:
http://localhost/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
http://localhost/mycakephpApp/typologies/index/index/img/uploads/typology/thumbnails/whatever.jpg
这是不存在的
有一个非常简单的解决方案:
- 始终使用绝对URL,即
/img/uploads/typology/thumbnails/where.jpg
- 使用助手函数和路由器类,或者至少识别它们的功能
$typologyThumbnails = Router::url('/' . $typologyThumbnails);
这将输出:
/mycakephpApp/img/uploads/typology/thumbnails/whatever.jpg
使用帮助程序和元素
查看表单的文件:
img/uploads/typology/thumbnails/whatever.jpg
echo "large chunk of html"
很难阅读/维护。相反,视图代码应为,例如:
<?php
foreach ($typologies as $typology) {
echo $this->element('typology', array('row' => $typology));
}
// pagination links
在路由选项中设置索引参数'action'=>'index','index'
,尝试不使用它,例如:路由器::连接('/',数组(admin'=>false,'controller'=>'类型','action'=>'index')代码>比很多,这真的很有帮助。