且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的负载更多的按钮,只有当有什么东西加载

更新时间:2023-11-28 23:43:10

 < PHP
    $的GetList =请求mysql_query(SELECT * FROM table_name的LIMIT 25);
    而($ GL = mysql_fetch_array($的GetList)){&GT?;
         < D​​IV CLASS = postitem ID =&LT ;?回声$ GL ['身份证'];?>>< PHP的echo $ GL ['标题']; ?>< / DIV>
    < PHP}
    如果(mysql_num_rows($的GetList)< = 25){>
    <脚本类型=文/ JavaScript的>
        $(函数(){
             $('#loadmorebutton)隐藏()。
        });
    < / SCRIPT>
    < PHP}&GT?;
 

I am currently using a script I found on hycus.com to create a load more on click feature on my page. When you click on the load more button, it fetches data from the server and displays it using ajax. It works fine but the problem I have is this...the load more button still shows up even when there's no data to show.

I want the load more button to show ONLY when there's data to show and disappear when there's no data to show.

index page: jquery

<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 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.

<?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 } ?>