admin管理员组文章数量:1420938
I have a script, see below:
Index page: jQuery script
<script type="text/javascript">
$(document).ready(function(){
$("#loadmorebutton").click(function (){
$('#loadmorebutton').html('<img src="ajax-loader.gif" />');
$.ajax({
url: "loadmore.php?lastid=" + $(".postitem:last").attr("id"),
success: function(html){
if(html){
$("#postswrapper").append(html);
$('#loadmorebutton').html('Load More');
}else{
$('#loadmorebutton').replaceWith('<center>No more posts to show.</center>');
}
}
});
});
});
</script>
HTML:
<div id="wrapper">
<div id="postswrapper">
<?php
$getlist = mysql_query("SELECT * FROM table_name LIMIT 25");
while ($gl = mysql_fetch_array($getlist)) { ?>
<div class=postitem id="<? echo $gl['id']; ?>"><?php echo $gl['title']; ?></div>
<?php } ?>
</div>
<button id="loadmorebutton">Load More</button>
</div>
</div>
The loadmore.php page has;
<?php
$getlist = mysql_query("SELECT * FROM table_name WHERE id < '".addslashes($_GET['lastid'])."'
LIMIT 0, 25 LIMIT 10");
while ($gl = mysql_fetch_array($getlist)) { ?>
<div><?php echo $gl['title']; ?></div>
<?php } ?>
Basically what it this script does is, the index page will load the first 25 items from the database, and when you click on load more, it triggers loadmore.php, which will load 10 more the data starting from the last id loaded already. What I want to do is to remove the "Load more" button from the screen IF there's less than 25 items in the database and show if there's more than 25 items in the database.
I have a script, see below:
Index page: jQuery script
<script type="text/javascript">
$(document).ready(function(){
$("#loadmorebutton").click(function (){
$('#loadmorebutton').html('<img src="ajax-loader.gif" />');
$.ajax({
url: "loadmore.php?lastid=" + $(".postitem:last").attr("id"),
success: function(html){
if(html){
$("#postswrapper").append(html);
$('#loadmorebutton').html('Load More');
}else{
$('#loadmorebutton').replaceWith('<center>No more posts to show.</center>');
}
}
});
});
});
</script>
HTML:
<div id="wrapper">
<div id="postswrapper">
<?php
$getlist = mysql_query("SELECT * FROM table_name LIMIT 25");
while ($gl = mysql_fetch_array($getlist)) { ?>
<div class=postitem id="<? echo $gl['id']; ?>"><?php echo $gl['title']; ?></div>
<?php } ?>
</div>
<button id="loadmorebutton">Load More</button>
</div>
</div>
The loadmore.php page has;
<?php
$getlist = mysql_query("SELECT * FROM table_name WHERE id < '".addslashes($_GET['lastid'])."'
LIMIT 0, 25 LIMIT 10");
while ($gl = mysql_fetch_array($getlist)) { ?>
<div><?php echo $gl['title']; ?></div>
<?php } ?>
Basically what it this script does is, the index page will load the first 25 items from the database, and when you click on load more, it triggers loadmore.php, which will load 10 more the data starting from the last id loaded already. What I want to do is to remove the "Load more" button from the screen IF there's less than 25 items in the database and show if there's more than 25 items in the database.
Share Improve this question edited Nov 14, 2011 at 13:44 Samuel Liew 79.2k111 gold badges169 silver badges304 bronze badges asked Nov 14, 2011 at 11:14 Jay SmokeJay Smoke 6043 gold badges16 silver badges38 bronze badges 1-
1
Wrap an if around the html.
<?php if(25 or more){ ?><button id="loadmorebutton">Load More</button><?php } ?>
– Matt Commented Nov 14, 2011 at 11:19
4 Answers
Reset to default 3This will work for you:
<?php
$getlist = mysql_query("SELECT * FROM table_name LIMIT 25");
while ($gl = mysql_fetch_array($getlist)) { ?>
<div class=postitem id="<? echo $gl['id']; ?>"><?php echo $gl['title']; ?></div>
<?php }
if(mysql_num_rows($getlist) <= 25) { ?>
<script type="text/javascript">
$(function(){
$('#loadmorebutton').hide();
});
</script>
<?php } ?>
<div id="wrapper">
<div id="postswrapper">
<?php
$getlist = mysql_query("SELECT * FROM table_name LIMIT 25");
**$cnt = 0;**
while ($gl = mysql_fetch_array($getlist)) { ?>
<div class=postitem id="<? echo $gl['id']; ?>"><?php echo $gl['title']; ?></div>
**$cnt++;**
<?php } ?>
</div>
**<?php if ($cnt == 24) { ?>
<button id="loadmorebutton">Load More</button>
<?php } ?>**
</div>
</div>
chagne your php script to with coount variable
<div id="wrapper">
<div id="postswrapper">
<?php
$count=0;
$getlist = mysql_query("SELECT * FROM table_name LIMIT 25");
while ($gl = mysql_fetch_array($getlist)) {
$count++;?>
<div class=postitem id="<? echo $gl['id']; ?>"><?php echo $gl['title']; ?></div>
<?php } ?>
</div>
<button id="loadmorebutton" value="<? echo $count ?>">Load More</button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#loadmorebutton").click(function (){
$('#loadmorebutton').html('<img src="ajax-loader.gif" />');
$.ajax({
url: "loadmore.php?lastid=" + $(".postitem:last").attr("id"),
success: function(html){
if(html){
$("#postswrapper").append(html);
var count = patserInt($('#loadmorebutton').val());
if(count<25){
$('#loadmorebutton').html('Load More');
}else {
$('#loadmorebutton').hide();
}else{
$('#loadmorebutton').replaceWith('<center>No more posts to show.</center>');
}
}
});
});
});
</script>
I'll give you two options, whichever is more readable to you.
<?php if(25 or more){ ?>
<button id="loadmorebutton">Load More</button>
<?php } ?>
<?php if(25 or more): ?>
<button id="loadmorebutton">Load More</button>
<?php endif; ?>
本文标签: javascriptJquery hideshow load more buttonStack Overflow
版权声明:本文标题:javascript - Jquery hideshow load more button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745339847a2654209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论