i have an ajax script and a php file on the server that inserts new post to the database, the ajax script can post to the target php file and get a response as json data (which is decoded and echo as HTML data which is my posts) which I use to append to a div. however, my solution returns the entire result from my database, ie all my posts that my query returns via mysql_query. how do i get only results that are recent or at least how do i get only my last post;
$(function() {
$("#PostItemz").on('submit', function(event){
event.preventDefault()
var formData = new FormData(this);
$.ajax({
url: 'inc/modules/post_data.php',
type: 'POST',
data: formData,
success: function (data) {
//here the data return is my post as html object
//data is return via echo on another php page
console.log(data);
$('#postarea').append(data);
$("#PostItemz")[0].reset();
$('.inner-addon, .left-addon, .create-text').hide();
},
cache: false,
contentType: false,
processData: false
});
});
});
AND this is my php code: on server that returns the post data:
<?php
function getFollowedGroupPosts($user_id){
$sql_query = mysql_query("SELECT gid FROM members WHERE uid = '$user_id'");
//iterate through record set, push records into user defined record_set[] array and return array as json encoded data
.
.
.
.
.
$record_set[] = array('gid' => $rows_b['gid'],
'pid' => $rows_b['pid'],
'post' => $rows_b['post'],
'images' => getPostImages($rows_b['pid']),
'jImages' => $url_string,
'videos' => getPostVideos($rows_b['pid']),
'audios' => getPostAudios($rows_b['pid']),
'date' => timeAgo($rows_b['date']),
'username' => userIdToName($rows_b['uid']),
'filemap_id' => $rows_b['filemap_id'],
'group_name' => groupIdToName($rows_b['gid']),
'comments' => getComments($rows_b['pid']),
'likes' => checkLikeCount($rows_b['pid']),
'dislikes' => checkDislikeCount($rows_b['pid']),
'post_count' => $row_count_b);
}
}
return $record_set;
how do i make async ajax calls to php and what query do i use on server side to return recent posts other than the ones on my page already
For any last inserted record will be get through mysql_insert_id If your table contain any AUTO_INCREMENT column it will return that Value.
<?php
$last_id = mysql_insert_id ( mysqli $link )
SELECT * FROM tbl WHERE pid(your primary key) = '$last_id'
?>
Also you can do that by using limit and order
SELECT * FROM `table_name`
ORDER BY `table_name`.`column_name` DESC
LIMIT 1
Note: Do not use mysql It deprecated from php 5.5.x
使用mysqli_insert_id()
方法获取最后插入的帖子的主键,然后使用该ID检索该特定帖子并从php脚本返回。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.