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

    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊