user3233664
user3233664

Reputation:

PHP/Ajax load more button

I want to display 5 items and when the user clicks the 'load more' button, 5 more items will be pulled out from the database.

I have my items being retrieved like so:

$res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 5") or die(mysql_error());

    if(mysql_num_rows($res) > 0){
        while($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $user_id = $row['user_id'];

then i have the ajax code and the button:

<script type="text/javascript">
    $(document).ready(function(){
        $(".load_more").click(function (){
            $('.load_more').html('<img src="images/ajax-loader.gif" />');
            $.ajax({
                url: "loadmore.php?lastid=" + ("<?php echo $id; ?>"),
                success: function(html){
                    if(html){
                        $(".main_page").append(html);
                        $('.load_more').html('Load More');
                    }else{
                        $('.load_more').replaceWith('<center>No more posts to show.</center>');
                    }
                }
            });
        });
    });
</script>



    <button class="load_more">Load More</button>

And finally the loadmore.php file that is being called when the button is clicked:

    $res17 = mysql_query("SELECT * FROM `posts` WHERE id < '".addslashes($_GET['lastid'])."' LIMIT 0, 25 LIMIT 10"); 
while($row17 = mysql_fetch_assoc($res17)){
            $id = $row17['id'];
            $user_id = $row17['user_id'];

how do i call the correct id? i know that url: "loadmore.php?lastid=" + (""), is probably wrong but not sure how to fix it

Upvotes: 1

Views: 2096

Answers (1)

Joe
Joe

Reputation: 33

I don't know if this was the issue but you forgot to close some of the opening curly brackets, I have fixed it here.

$res = mysql_query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 5") or die(mysql_error());

    if(mysql_num_rows($res) > 0){
        while($row = mysql_fetch_assoc($res)){
            $id = $row['id'];
            $user_id = $row['user_id'];
}}

Upvotes: 1

Related Questions