Welcome Guest, Not a member yet? Register   Sign In
php how to update timestamp on post edit?
#1

[eluser]Unknown[/eluser]
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:
Code:
$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:

Code:
[removed]
        $(document).ready(function(){
            $(".load_more").click(function (){
                $('.load_more').html('<img src="images/ajax-loader.gif" />');
                $.ajax({
                    url: "loadmore.php?lastid=" + ("&lt;?php echo $id; ?&gt;"),
                    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>');
                        }
                    }
                });
            });
        });
    [removed]



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

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

Code:
$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=" + ("&lt;?php echo $id; ?&gt;"), is probably wrong but not sure how to fix it
#2

[eluser]Tpojka[/eluser]
Quick solution. Try just:
Code:
url: "loadmore.php?lastid="&lt;?php echo $id; ?&gt;,
#3

[eluser]jonez[/eluser]
If you are pulling items via AJAX you can't use PHP variables to maintain the last ID (that is, unless you are removing and reinjecting your script every time). In the callback set a JS variable to the last ID you pulled, then use that ID in the URL for the second AJAX call.

It would be something like this:
Code:
$(document).ready(function(){

var lastId = 0;

            $(".load_more").click(function (){
                $('.load_more').html('<img src="images/ajax-loader.gif" />');
                $.ajax({
                    url: "loadmore.php?lastid=" + lastId,
                    success: function(response){
                        if(response.html){

                            lastId = response.last_id;

                            $(".main_page").append(response.html);
                            $('.load_more').html('Load More');
                        }else{
                            $('.load_more').replaceWith('<center>No more posts to show.</center>');
                        }
                    }
                });
            });
        });
You'll need to modify what your controller outputs so it passes back a blob of HTML and a record ID.




Theme © iAndrew 2016 - Forum software by © MyBB