2014-04-16 36 views
0

到目前爲止,我可以通過設置靜態限制來加載結果。但對於我的puroposes,我需要這個限制可以通過一個函數來設置,該函數每次都會產生一個介於2到8之間的數字,以便有更多結果的新調用(我已經完成了該函數,但不知道它在代碼中的位置)如何設置cakephp分頁記錄限制動態

function random_cols() { 
$min_cols=2; 
$max_cols=8; 
$cols = array(); 
$n = rand($min_cols,$max_cols); 
array_push($cols, $n); 
$var = 12 - $n; 
while ($var>0) { 
    $n = rand($min_cols,$var); 
    if ($var-$n<=1) { 
    $n = $var; 
    } 
$var = $var - $n; 
array_push($cols, $n); 
} 
return $cols; 
} 

$mylimit_arr = random_cols(); 
$mylimit = count($mylimit_arr); 

控制器:

public $paginate = array(
     'limit' => $mylimit, 
     'order' => array('Post.id' => 'asc'),  
); 

public function index() { 

    $posts = $this->paginate('Post'); 
     $this->set('posts', $posts); 

} 

視圖(使用infinitescroll插件):

<div id="posts-list"> 
<div class="post-item"></div> 
</div> 

<script> 
    $(function(){ 
    var $container = $('#posts-list'); 

    $container.infinitescroll({ 
     navSelector : '.next', // selector for the paged navigation 
     nextSelector : '.next a', // selector for the NEXT link (to page 2) 
     itemSelector : '.post-item',  // selector for all items you'll retrieve 
     debug   : true, 
     dataType  : 'html', 
     loading: { 
      finishedMsg: 'No more posts to load. All Hail Star Wars God!', 
      img: '<?php echo $this->webroot; ?>img/spinner.gif' 
     } 
     } 
    ); 
    }); 

</script> 

你可能想知道,爲什麼我需要的,但它的設計和方式,因爲我顯示結果

回答

1
$this->paginate = array('limit' => $mylimit); 

的伎倆。

你的控制器看起來應該是這樣:

public function index() { 
    $mylimit_arr = random_cols(); 
    $mylimit = count($mylimit_arr); 
    $this->paginate = array(
    'order' => array('Post.id' => 'asc'), 
    'limit' => $mylimit 
); 
    $posts = $this->paginate('Post'); 

    $this->set('posts', $posts); 
} 
+0

你好,對不起,你能更好的解釋嗎? :) –

+0

現在清楚了嗎? –