0

I trying to order a query inside a foreach loop. After looking around reasoning why its not displaying the results correctly I've read that the foreach will only run once for every user its found so its basically just ordering that one users updates.

$AllFriend = explode(",", $UserInfo['friends']);
                                array_push($AllFriend, $Username);

                                foreach ($AllFriend as $FriendSel) {

                                    $sql5 = "SELECT * FROM updates WHERE username = :username ORDER BY id DESC LIMIT 20";
                                    $DoQuery = $pdo->prepare($sql5);
                                    $DoQuery->bindValue(':username', $FriendSel);
                                    $DoQuery->execute();
while ($Updates = $DoQuery->fetch(PDO::FETCH_ASSOC)){

I have the friends in the database like "friend1, friend2, friend3" etc so I'm using array_push to add the users username in to be able to see their own updates to as you wouldn't add yourself as a friend.

What would I need todo to be able to get the updates ordering by ID.

I'm not wanting someone to just show me what todo and thats it as I want to learn so pointing me in the right direction would be great!

Thank you!


As an update, I've got an array what holds the usernames and have changed the query

 $FriendNames = array();


                                $AllFriend = explode(",", $UserInfo['friends']);
                                array_push($AllFriend, $Username);

                                foreach ($AllFriend as $FriendSel) {
                                    $FriendNames[] = $FriendSel;
                                }
                                $Addit = implode(",", $FriendNames);
 $sql5 = "SELECT * FROM updates WHERE username IN ('$Addit') ORDER BY id DESC";
                                $DoQuery = $pdo->prepare($sql5);                                $DoQuery->execute();
while ($Updates = $DoQuery->fetch(PDO::FETCH_ASSOC)) {

The $Addit variable is holding usernames like "friend1,friend2,friend3" etc although now my while loop is blank and showing no results. For what reason is this?

Thanks

Dean7
  • 1
  • 2
  • You need something more like `SELECT * FROM updates WHERE username IN ( ??? ) ORDER BY id DESC LIMIT 20`, where `???` is a list of usernames. To dynamically create that with PDO, see https://stackoverflow.com/questions/14767530/php-using-pdo-with-in-clause-array. And of course not in a loop, but as a one-time query. – kmoser Aug 31 '21 at 19:14
  • I did use the ```In ( :username)``` part before hand but tried different things to see if I could get it working before asking here. How could I do it as a one time query if I need the loop to loop all the usernames in the array ? – Dean7 Aug 31 '21 at 19:20
  • See if [this information](https://phpdelusions.net/pdo#in) may help? – Paul T. Sep 01 '21 at 01:44
  • @Dean7 The whole point of `IN (...)` is that it lets you specify multiple values. After doing that one query, you then fetch the results in a loop as you're already doing. – kmoser Sep 01 '21 at 04:25
  • So I need todo the query before doing the foreach loop? – Dean7 Sep 01 '21 at 19:52

0 Answers0