【发布时间】:2013-02-22 15:22:47
【问题描述】:
我在 codeigniter 中使用 jquery 插件 scrollpagination 我面临的问题是我的循环没有终止并且没有给出准确的结果。
这是我的 html 代码
<div id="main">
<div id='friend_display'>
<?php if($list->num_rows() > 0 ){
foreach($list->result() as $show)
{ ?>
<div class="image-box" style="margin-left:30px" id='image-holder' >
<div class="photo-cover">
<a href="<?=base_url()?>uploads/user_images/<?php echo $show->user_image;?>" class="big-image"><img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" /></a>
</div>
<p class="photo-name"><b><?php echo $show->user_name;?></b></p>
</div>
<?php } } else { echo '<div align="center" style="color:#FF0000; font-size:17px; font-weight:bold">You have no Friends yet</div>';}?>
<div class="cl"> </div>
</div></div>
这是脚本
<script type="text/javascript">
var page_num = 1;
$(function(){
$('#friend_display').scrollPagination({
'contentPage': '<?=base_url()?>friends/load_more', // the url you are fetching the results
'contentData': {page_num:$('.image-box').size()}, // these are the variables you can pass to the request, for example: children().size() to know which page you are
'scrollTarget': $(window), // who gonna scroll? in this example, the full window
'heightOffset': 10, // it gonna request when scroll is 10 pixels before the page ends
'beforeLoad': function(){ // before load function, you can display a preloader div
$('#loading1').fadeIn();
},
'afterLoad': function(elementsLoaded){ // after loading content, you can use this function to animate your new elements
$('#loading1').fadeOut();
var i = 0;
$(elementsLoaded).fadeInWithDelay();
page_num:$('.image-box').size();
}
});
// code for fade in element by element
$.fn.fadeInWithDelay = function(){
var delay = 0;
return this.each(function(){
$(this).delay(delay).animate({opacity:1}, 200);
delay += 100;
});
};
});
</script>
这是我的 php 函数
function load_more()
{
$offset = $this->input->post('page_num');
$list = $this->friends_model->show_friends($offset);
if($list->num_rows()>0)
{
foreach($list->result() as $show)
{?>
<div class="image-box" style="margin-left:30px" id='image-holder'>
<div class="photo-cover">
<a href="<?=base_url()?>uploads/user_images/<?php echo $show->user_image;?>" class="big-image"><img width="160px" height="117px" src="<?=base_url()?>uploads/user_images/friends/<?php echo $show->user_image;?>" alt="" /></a>
</div>
<p class="photo-name"><b><?php echo $show->user_name;?></b></p>
</div>
<?php } ?>
<div class="cl"> </div>
<?php
}
else
{
//echo(333);
}
}
在 db 中我只支持主查询 $this->db->limit(12,$offset); 谁能告诉我我错过了什么?
打开此链接查看完整代码。Scroll Pagination
【问题讨论】:
-
你的问题不够详细。哪个循环失败,失败的结果是什么,最重要的是what have you tried?
-
问题是当我向下滚动时它会显示结果并且不会停止显示。假设在我的数据库中我有 50 个结果然后我首先使用 10 个结果显示然后当滚动分页调用进行时它显示 jst 10 依此类推首先显示 jst 10 而不是其他 40。如果我向下滚动它连续显示我 jst 10 循环不会结束结果
-
听起来好像您的偏移值没有正确管理。确保
var_dumping 正确传递了该值并更改它直到它工作为止。 -
问题是偏移值,在我的 js 中 page_num 没有返回准确的偏移结果
-
@Robin Castlin 打开链接并查看完整代码以了解我想说的我更新了问题并添加了链接
标签: php jquery codeigniter infinite-scroll