dimanche 2 juin 2019

Finding related users that matching a condition

Guys Im trying to show posts to user based on previous posts that the user liked ,seen and some followers he has. I have 3 for loops that I am checking these conditions. in the 3rd one we check if user_Id is in latest_post_seen , follower and like tables if it is we pass it to another variable and unset that BUT the problem is if I set the limit by 100 for each query , the 2nd and 3rd loop will go throw for each item in 1st loop, so 200 loops for each , would be 20,000 loops which drives me crazy!

this is the code i comeup with

$onlyDLS are the recent posts our user checked (limit 100)

$onlyLike is a query with posts he liked ( limit 100)

$followers is all the users that our user is following

 foreach($followers as $indexf=>$follower){     
        foreach( $onlyDLS as $index=>$each){
        foreach( $onlyLike as $indexl=>$eachl){
            if($follower->following_id == $index && $index == $indexl){
                $withdlsLike->push($index);//if we had follower with dls , index is user_id, and we had the user in the like
                //SINCE we are sorting the users with  posts (arsort) withdls with be in DESC order having the users with more post at top
                $flagger = true;
                    unset($onlyLike[$indexl]);
                    unset($onlyDLS[$index]);
            }
        }
        if($follower->following_id == $index){// if user_Id was not found in like then this if will be checked
                $withdlsLike->push($index);//if we had follower with dls , index is user_id

                $flagger = true;
                    unset($onlyDLS[$index]);
            }
            if($flagger == false){//if we didnt find the user_id in dls then its only a follower
                $onlyfollow->push($follower->following_id);
                unset($follower[$indexf]);
            }
        }

so each loop has 100 items at max , eachtime we found a match we unset those we have to. The first time its 20,000 , if there was not a match again 20,000.There must be a better way which I dont know.

Any suggestion is highly appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire