0

i have a question about instagram style infinite scroll system. I am trying to make that page. They are showing the posts randomizely but i am confused about infinite scroll part. I have created this query:

$morequery=" AND p.post_id<'".$lastpost."' ";

SELECT DISTINCT p.post_id,
                p.user_id, 
                u.username 
FROM  posts p, users Uu 
WHERE u.ustatus='1' AND 
      p.userid_fk =u.user_id AND 
      $morequery ORDER BY p.post_id DESC LIMIT

The query working like when user scroll down the ajax code sending lastpost id and query working with the lastpost. But it is not ramdom system i just use the DESC in this query. If i change the query ORDER BY p.post_id DESC LIMIT to ORDER BY RAND() LIMIT when page open rand() working but scroll is not working.

I have searched some solution on stackoverflow. They said use session like:

$_SESSION['seen_scroll_posts'] = array();
if (!empty($_SESSION['seen_scroll_posts'])) {
  $morequery = " AND post_id NOT IN (" . implode($_SESSION['seen_scroll_posts']) . ") ";
}

This is not a good solution because if user scroll down many times then the page going to be slow. Also the post_id is primary key if i use session solution the page going to be slow.

Is there anyway to show posts randomly like instagram explore? In this regard, do you have a suggestion or solution path ?

AlwaysStudent
  • 1,354
  • 18
  • 49
  • What have you tried to debug the problem? Why should the raw scrolling and loading functionality in the frontend be affected by that SQL query? Have you checked whether that query is valid? – Nico Haase Jun 18 '19 at 08:48
  • use random https://stackoverflow.com/a/4329447/5503275 – DsRaj Jun 18 '19 at 08:50
  • 1
    `ORDER BY RAND()` will result in a _different_ order each time you execute that query. Depending on the specifics of your infinite scroll solution, it might fail if it encounters the same post, element id or whatever twice. You should at least use this with a fixed seed value to guarantee the same random ordering is used across all those queries. https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_rand – 04FS Jun 18 '19 at 09:14

0 Answers0