2012-11-19 3 views
1

나는 cakephp 2+ 프로젝트에서 일하고 있습니다. 두 개의 왼쪽 및 오른쪽 div 조합으로 제품 목록 정렬을위한 페이지 매김을 구현 중입니다. 왼쪽 div를 만들 수 있지만 오프셋이 페이지 매김으로 설정 될 수 없으므로 오른쪽 div를 만들 수 없습니다. 왼쪽 div에 절반 항목이 필요하고 오른쪽 div에 절반 항목이 필요하므로 한계를 설정할 수는 있지만 상쇄 할 수는 없습니다. 내가 어떻게 할 수 있니?Cakephp 오른쪽 div 조합으로 페이지 매김

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 

답변

0

$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> 

또 다른 방법은 컨트롤러에 array_chunk을 사용하는 것입니다 오른쪽에서 왼쪽 열 다섯의 5 개 항목을 원하는 말. 이 핵심 PHP 함수를 사용하면 다차원 숫자 인덱스 배열로 끝나고 반복 할 수 있고 관련 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>'; 
    } 
?> 
+0

정말 정확한 답장을 보내 주셔서 감사합니다. 거의 끝났습니다 ...... 나중에 참조 할 코드를 거의 넣을 것입니다 ......... 다시 한번 감사드립니다. 내 repu가 아직 이것을 허용하지 않으므로 죄송합니다. –

관련 문제