The problem here is the result is always descending by id in every group of userID
.
$ids = $_SESSION['userid'];
$uosql = "SELECT userone FROM friends WHERE usertwo='$ids'";
$uoresult = mysqli_query($conn, $uosql);
while($uorow = mysqli_fetch_assoc($uoresult)){
$userone = $uorow['userone'];
$postsql = "SELECT * FROM userspost WHERE userID IN($userone) ORDER BY id DESC";
$postsqlres = mysqli_query($conn, $postsql);
while($postrow = mysqli_fetch_assoc($postsqlres)){
echo $postrow['post']."<br>";
}
}
FIRST OUTPUT:
marvs post 3
marvs post 2
marvs post 1
juan post 2
juan post 1
This is the output I was expecting:
$ids = $_SESSION['userid'];
$uosql = "SELECT userone FROM friends WHERE usertwo='$ids'";
$uoresult = mysqli_query($conn, $uosql);
while($uorow = mysqli_fetch_assoc($uoresult)){
$userone = $uorow['userone'];
$postsql = "SELECT * FROM userspost WHERE userID IN(1,4) ORDER BY id DESC";
$postsqlres = mysqli_query($conn, $postsql);
}
while($postrow = mysqli_fetch_assoc($postsqlres)){
echo $postrow['post']."<br>";
}
SECOND OUTPUT:
marvs post 3
juan post 2
marvs post 2
marvs post 1
juan post 1
I tried this kind of code because I also want to be like this kind of OUTPUT. Needs to be included my id session.
$ids = $_SESSION['userid'];
$uosql = "SELECT userone FROM friends WHERE usertwo='$ids'";
$uoresult = mysqli_query($conn, $uosql);
while($uorow = mysqli_fetch_assoc($uoresult)){
$userone = $uorow['userone'];
$postsql = "SELECT * FROM userspost ORDER BY id DESC";
$postsqlres = mysqli_query($conn, $postsql);
}
while($postrow = mysqli_fetch_assoc($postsqlres)){
echo "id ".$postrow['id']."| userID ".$postrow['userID']."| post ".$postrow['post']."<br>";
}
THIRD OUTPUT: Please note: The numbers in the post is just a number that posted/uploaded in the userspost table together with words.
id 8| userID 1| post marvs post 3
id 7| userID 2| post abegs post 3
id 6| userID 4| post juan post 2
id 5| userID 1| post marvs post 2
id 4| userID 2| post abegs post 2
id 3| userID 1| post marvs post 1
id 2| userID 4| post juan post 1
id 1| userID 2| post abeg post 1
DATABASE STRUCTURE friends
id | userone | usertwo
23 | 4 | 1
24 | 1 | 2
25 | 4 | 2
DATABASE STRUCTURE userspost
id | userID | post | uploaddate | uploadtime | file
1 | 2 | abeg post 1 |
2 | 4 | juan post 1 |
3 | 1 | marvs post 1 |
4 | 2 | abegs post 2 |
5 | 1 | marvs post 2 |
6 | 4 | juan post 2 |
7 | 2 | abegs post 3 |
8 | 1 | marvs post 3 |