colap Posted August 29, 2015 Share Posted August 29, 2015 (edited) <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 August 29, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/ Share on other sites More sharing options...
Ch0cu3r Posted August 29, 2015 Share Posted August 29, 2015 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++; } } Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519886 Share on other sites More sharing options...
CroNiX Posted August 29, 2015 Share Posted August 29, 2015 Since this is a comparison, you need to use double == here instead of assigning the value: if($_POST['page'] = $total_number_of_page) { Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519900 Share on other sites More sharing options...
colap Posted August 31, 2015 Author Share Posted August 31, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519977 Share on other sites More sharing options...
Ch0cu3r Posted August 31, 2015 Share Posted August 31, 2015 If the data.length = 0 , then there will be no button Why would there be no button? Is there more code that removes the button? Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519978 Share on other sites More sharing options...
colap Posted August 31, 2015 Author Share Posted August 31, 2015 Why would there be no button? Is there more code that removes the button? Button is not necessary if there is not more data. No more code. Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519979 Share on other sites More sharing options...
Ch0cu3r Posted August 31, 2015 Share Posted August 31, 2015 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519980 Share on other sites More sharing options...
colap Posted August 31, 2015 Author Share Posted August 31, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/297976-how-can-i-check-that-it-is-the-last-record/#findComment-1519981 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.