I am trying to bring out the news of those people that the user is subscribed to, but I get porridge. No, everything is output, but the problem is that they are not outputted by ORDER BY id DESC .

Here bd, look in what order the news:

I try to output it through ORDER BY id DESC , but it turns out like this: enter image description here

I understand what the problem is, but I absolutely do not understand how to solve it and why this happens. while should not give two values ​​at once in $query .

And yet: is it possible to do this using one cycle?

Here is the code itself:

 // Выбираем всех пользователей на которых Мы подписаны. $sql = "SELECT * FROM follow WHERE sender='$s_id'"; $sql = mysqli_query($connect,$sql); $row = mysqli_num_rows($sql); if ($row) { while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)) { // Получаем их. $target = $row['target']; // Получаем новости, авторы которых пользователи, на которых мы подписаны. // Тут и возникает проблемка. $target почему-то сразу забирается во все поля где он //есть в shots , а не одно. $query = "SELECT * FROM shots WHERE user_id='$target' AND access='1' ORDER BY id DESC "; $query_go = mysqli_query($connect,$query); $row_shot = mysqli_num_rows($query_go); if ($row_shot) { while($row_shot = mysqli_fetch_array($query_go,MYSQLI_ASSOC)) { $title = $row_shot['title']; $image = $row_shot['image']; echo " <div class=\"col-xs-12 col-sm-6 col-md-6 col-lg-4\"> <div class=\"shot\" data-id=\"\"> <div class=\"shot-preview\"> <a class=\"img\" href=\"shot?id=\"> <img src=\"$image\" alt=\"\"> </a> <h5 class=\"title\"> <a href=\"shot-gallery.html\">$title</a> </h5> <a class=\"label\" href=\"page-search.html\">App</a> </div> <div class=\"shot-detail\"> <div class=\"shot-info\"> <a href=\"\"> <img src=\"assets/img/guest3.png\" alt=\"avatar\"> </a> <h6> <a href=\"\">$target</a> </h6> <time datetime=\"2016-02-04 22:30\">2 hours ago</time> </div> <ul class=\"shot-stats\"> <li> <i class=\"fa fa-eye\"></i> <span>1</span> </li> <li> <a href=\"shot?id=\"> <i class=\"fa fa-comments\"></i> <span>1s</span> </a> </li> </ul> </div> </div> </div> "; } } } } 
  • You take one person for whom there is a subscription - all his news is displayed, then the second - his news, then the third - his news. The result is 2, 2, 4, 4, 3, 3 (two news from each user). So how should it be? To have one last news from each? From the question is not entirely clear what result you want to achieve. - cheops
  • It is necessary that all the news of users are displayed, starting with the latest. That is, that would not be: News5 News3 News6 News1 News4 News2, and News6 News5 News4, etc. - Alexandra Kott

1 answer 1

In order to display news in the order sorted by id, you can use a two-table query for the follow and shorts tables. By the way, this will solve the nested loops problem, since you will have one query and one result table. You can push off from the next request

 SELECT f.target AS target, s.id AS id, s.title AS title, s.image AS image FROM follow AS f JOIN shots AS s ON s.user_id = f.target WHERE f.sender = '$s_id' AND s.access = '1' GROUP BY s.id ORDER BY s.id DESC 
  • Thank you thank you thank you! Exactly what is needed. - Alexandra Kott