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; ?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339847a4623274.html
评论列表(0条)