Jump to content

How can i check that it is the last record?


colap

Recommended Posts

<script type=text/javascript src="jquery-1.11.3.js"></script>
<input type="button" id="id_load_more" name="nm_load_more" value="Load More" />
<script type="text/javascript">
    $(document).ready(function() {
        var limit = 1;   
        console.log("ready!");   
        $( "input#id_load_more" ).on( "click", function( event ) {
              //event.stopPropagation();   
            $.ajax({
                method: "POST",
                url: "load_more.php",
                data: {"page": limit},
                dataType: "JSON",
                success: function(data) {
                    console.log(data);
                    //console.log(data.status);
                    limit++;
                }
            });
       
        });
    });
</script>

If there is no data in table limit++ needs not to increment. How can i do it?

This is the load_more.php

<?php

if (session_id() == '') {
    session_start();
}

require_once ('./functions.php');

$dbh = mysql_connection();
$user_id = $_SESSION['login_user_id'];

$sql_total_rows = "SELECT u.id, u.username as non_friend
FROM users u
LEFT JOIN
    (
        SELECT user_id, friend_id
        FROM friends
        WHERE user_id = $user_id

            UNION

        SELECT friend_id, user_id
        FROM friends
        WHERE friend_id = $user_id
       
    ) f  ON u.id = f.friend_id
WHERE u.id <> $user_id
     AND f.friend_id IS NULL
ORDER BY id";
$stmt_total_rows = $dbh->prepare($sql_total_rows);
$stmt_total_rows->execute();
$result_total_rows = $stmt_total_rows->rowCount();
//echo $result_total_rows;

$number_of_items_per_page = 2;
$total_number_of_page = ceil($result_total_rows/$number_of_items_per_page);
//echo $total_number_of_page;

//$page = $_POST['page'];
//$_POST['page'] = 2;
$limit = ($_POST['page'] - 1) * $number_of_items_per_page;

$sql_non_friends = "SELECT u.id, u.username as non_friend
FROM users u
LEFT JOIN
    (
        SELECT user_id, friend_id
        FROM friends
        WHERE user_id = $user_id

            UNION

        SELECT friend_id, user_id
        FROM friends
        WHERE friend_id = $user_id
       
    ) f  ON u.id = f.friend_id
WHERE u.id <> $user_id
     AND f.friend_id IS NULL
ORDER BY id limit $limit , $number_of_items_per_page;";

//echo $sql_non_friends;

$stmt = $dbh->prepare($sql_non_friends);
$stmt->execute();
$result = $stmt->fetchAll();
//formatted_value($result);

/*
foreach ($result as $value) {
    //formatted_value($value);
    $user_id = $value['id'];
    echo $value['non_friend'];
    echo "<a href='add_friend.php?user_id=$user_id' >Add Friend</a>";
    echo '<br/>';
}
*/
//echo $_POST;
//echo json_encode($_POST);
if($_POST['page'] = $total_number_of_page) {
    $data['status'] = 1;
    $data['result'] = $result;
    echo json_encode($data);
} else {
    echo json_encode($result);
}

It is a ajax request to do load more data after clicking the button.

Thanks in advance.

Edited by Ch0cu3r
Link to comment
Share on other sites

1.  $dbh->prepare()  is for initializing a prepared statement. For each values used in the query you should be calling bindParam

 

2. Have your php script to not output the result if we are on the last page. In your javascript code you will only increment limit when data is not empty

// only output the result if we are not on the last page
if($_POST['page'] != $total_number_of_page)
{
    echo json_encode($result);
}

In your javascript

                success: function(data) {
                    // data is not empty, increment limit
                    if(data.length != 0) {
                        limit++;
                    }
                }
Link to comment
Share on other sites

 

1.  $dbh->prepare()  is for initializing a prepared statement. For each values used in the query you should be calling bindParam

 

2. Have your php script to not output the result if we are on the last page. In your javascript code you will only increment limit when data is not empty

// only output the result if we are not on the last page
if($_POST['page'] != $total_number_of_page)
{
    echo json_encode($result);
}

In your javascript

                success: function(data) {
                    // data is not empty, increment limit
                    if(data.length != 0) {
                        limit++;
                    }
                }
<input type="button" id="id_load_more" name="nm_load_more" value="Load More" />

If the data.length = 0 , then there will be no button. How can i do this?

Link to comment
Share on other sites

 

Sorry misread your last post. To remove the button you should be able to do

                success: function(data) {
                    // data is not empty, increment limit
                    if(data.length != 0) {
                        limit++;
                    } else {
                        $(this).hide();
                    }
                }

It hides the button, but there is one extra request from button to load data. How can i stop it? How can i let jquery know that it is the last record so hide the button?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.