到目前为止,我可以通过设置静态限制来加载结果。但出于我的目的,我需要一个函数可以设置该限制,每次有新的调用来获取更多结果时,该函数都会生成 2-8 之间的数字(我已经完成了该函数,但不知道将其放在代码中的位置) )
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);
}
视图(使用无限滚动插件):
<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>
您可能想知道为什么我需要它,但这是因为设计和我显示结果的方式
$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);
}