duanbarong4617 2017-04-24 08:39
浏览 37

根据条件更改加载更多按钮值

Hi I am using load more button to load content of the next page on the same web page. It can display unlimited no of results on the same page. Here is the code:

HTML:

<div class="wrapper">
    $results = $mysqli->prepare("SELECT id, name, message FROM paginate ORDER BY id DESC LIMIT ?, ?");
    $results->bind_param("dd", $position, $item_per_page);
    $results->execute(); //Execute prepared Query
    $results->bind_result($id, $name, $message);

    while($results->fetch()){ //fetch values
        echo '<li>'.$id.') <strong>'.$name.'</strong> : '.$message.'</li>';
    }
    <ul id="results"></ul>
    <div align="center">
    <button id="load_more_button"><img src="ajax-loader.gif"  class="animation_image" style="float:left;"> Load More</button> <!-- load button -->
    </div>
</div>

Jquery var track_page = 1;

    $("#load_more_button").click(function (e) {
        track_page++;
        load_contents(track_page);
    });

    function load_contents(track_page){
        $('.animation_image').show();       
        $.post( 'fetch_pages.php', {'page': track_page}, function(data){
           if(data.trim().length == 0){
                $("#load_more_button").text("No more records!").prop("disabled", true);
            }

            $("#results").append(data);

            $('.animation_image').hide();
        });
    }
</script>

fetch_pages.php

<?php
include("config.inc.php");
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
if(!is_numeric($page_number)){
    header('HTTP/1.1 500 Invalid page number!');
    exit();
}
$position = (($page_number-1) * $item_per_page);
$results = $mysqli->prepare("SELECT id, name, message FROM paginate ORDER BY id DESC LIMIT ?, ?");
$results->bind_param("dd", $position, $item_per_page);
$results->execute();
$results->bind_result($id, $name, $message);


while($results->fetch()){
    echo '<li>'.$id.') <strong>'.$name.'</strong> : '.$message.'</li>';
}

What i want to do is to change Load More button value accordingly ?

  1. If there are no results then display NO RESULTS ! in load more button.
  2. If there are less than 6 results then first display LOADING DATA in load more button until the data loads and then display END OF RESULTS ! in load more button.
  3. AND if there are more then 5 results then first display LOADING DATA and then display LOAD MORE after that.

Here is what i have tried so far and as a result it becomes more confusing for visitors as it display END OF RESULTS! until data loads.

    <?php
        if((results)<5){
    ?>
            <div align="center" class="load-more-b">
                <button id="load_more_button">END OF RESULTS !</button>
            </div>
    <?php
        }else{
    ?>
            <div align="center" class="load-more-b">
                <button id="load_more_button"><img src="ajax-loader.gif"  class="animation_image" style="float:left;">&nbsp;&nbsp;LOAD MORE</button> 
            </div>
    <?php
        }
    ?>

I know i need to make changes in jquery code to achieve this, but i don't have enough knowledge about jquery. So please Any help is appreciated. Thanks.

EDIT: Now i am displaying my first page without AJAX call and to display second page and so on i am using AJAX call. Now the problem is, If there are more than 5 results, It displays LOAD MORE button with GIF image. But i want only LOAD MORE button without GIF And when user click on that button then GIF image to be displayed until the next results load. Thanks... Its working very well for me. But my problem is when i use this script it takes 2-3 seconds to load data. And during these 2-3 seconds Load More displays on the screen.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP