weixin_33730836 2015-10-11 11:25 采纳率: 0%
浏览 34

AJAX PHP显示更多按钮

I've been looking online and on StackExchange for this answer, but can't seem to get my head around it.

I am wanting to have a button on my website that when clicked, will load more reviews. Currently I am limiting the reviews to 10 per page.

I have managed to get the button to display when there are more than 10 reviews, but I am stuck on what to do with this button to actually get it to load more reviews.

Would someone be able to help me with this please?

Here's my code:

Review Page

    <?php
    require('../../private_html/db_connection/connection.php');
    $id= $_GET['id'];
    $start = 0;
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $sql = "SELECT reviews.*, user_accounts.display_name, user_accounts.profile_picture FROM reviews, user_accounts WHERE reviews.company_id=$id && user_accounts.account_number = reviews.account_number ORDER BY time_stamp DESC LIMIT $start,10";
        $reviews = $conn->query($sql);
        foreach ($reviews as $row) {
            print "//--DISPLAY FORMAT GOES HERE --/";
        }
        $dbh = null;
    }
    catch (PDOexception $e) {
        echo "Error is: " . $e-> etmessage();
    }
    ?><?php
     $offset=$start+10;
if($reviews->rowCount() > $offset){
echo "<button id='loadmore' name='loadmore'>Load More Reviews</button>";
}
?>  
  • 写回答

2条回答 默认 最新

  • weixin_33697898 2015-10-11 11:40
    关注

    Something like this should do the job:

        <script>
        //this uses jQuery
        $("#loadmore").click(function(e) {
           $("#commentsdiv").append("<div></div>").load("get_more_comments.php?start=10&limit=10");
        });
        </script>
    

    What it does is : when you click the "loadmore" button it will call get_more_comments.php and append the result to the "commentsdiv" div. You just need to track what comments you have loaded so far and pass that to the JavaScript function when fetching the data (see the star, limit vars in the query).

    评论

报告相同问题?