I am working in a cakephp 2+ project. I am implementing pagination for sorting product listing in two left and right div combination. I am able to make left div but could not make right one since offset can not be set in pagination. I need half items in left div and half items in right div, so I can set limit but not able to offset. How can i do this?
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
Why not call
$this->paginate()once and loop over all the items and perform the split in the View itself? Performing both calls is rather wasteful of database resources.In that case you would have have a call to $this->paginate in the Controller. Say you want five items in the left column and five in the right:
In the view:
Another way would be to use
array_chunkin the Controller. Using this core PHP function you’ll end up with multidimensional numerically indexed array which you can loop over and wrap the child arrays in their relevant divs.