Pagination Cakephp分页与左右div组合
我在一个cakephp 2+项目中工作。我正在实现分页,以便以两个左div和右div组合对产品列表进行排序。我可以设置左div,但无法设置右div,因为无法在分页中设置偏移量。我需要在左div和右div的一半项目,所以我可以设置限制,但不能抵消。我该怎么做Pagination Cakephp分页与左右div组合,pagination,cakephp-2.0,Pagination,Cakephp 2.0,我在一个cakephp 2+项目中工作。我正在实现分页,以便以两个左div和右div组合对产品列表进行排序。我可以设置左div,但无法设置右div,因为无法在分页中设置偏移量。我需要在左div和右div的一半项目,所以我可以设置限制,但不能抵消。我该怎么做 Controller code public function index() { $rows=$this->Product->find('count', array('conditions'=>array('Produ
Controller code
public function index()
{
$rows=$this->Product->find('count', array('conditions'=>array('Product.allow'=>1)));
if($rows%2==0)
{
$this->paginate = array('conditions' => array('Product.allow'=>1,'limit'=>($rows/2));
$list_l = $this->paginate('Product');
$this->set('left_list',$list_l);
$this->paginate = array('conditions' => array('Product.allow'=>1,'limit'=>($rows/2), 'offset'=>$rows/2));
$list_r = $this->paginate('Product');
$this->set('right_list',$list_r);
}
else
{
$right_list=$this->Paginate('Product', array('Product.allow'=>1),array('limit'=>($rows-round($rows/2)), 'offset'=>round($rows/2)));
}
}
View Code
Foreach loop with array returned from controller
为什么不调用$this->paginate一次,循环所有项目,并在视图本身中执行拆分?执行这两个调用相当浪费数据库资源
在这种情况下,您需要在控制器中调用$this->paginate。假设您希望左栏中有五项,右栏中有五项:
$products = $this->paginate = array('conditions' => array('Product.allow'=>1, 'limit' => 10));
$this->set('products', $products);
他认为:
<div class="left-column">
<?php
foreach ($products as $product) {
debug($product);
if ($count === 5) {
echo "</div>\n<div class=\"right-column\">";
$count = 1;
}
$count++;
}
?>
</div>
感谢曼施的准确回复。我快做完了。。。。。。将把代码放在近……的地方供将来参考。。。。。。。。。再次感谢抱歉不能投票给你,因为我的共和国还不允许这样做;
<?php
$limit = round(count($products)/2);
$products = array_chunk($products, $limit);
foreach ($products as $index=>$groupedProducts) {
echo ($index === 0) ? '<div class="left-column">': '<div class="right-column">';
foreach ($groupedProducts as $product) {
debug($product);
}
echo '</div>';
}
?>