Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to make a follower post kind of thing in PHP and whenever Ajax refreshes my page, they are not in the correct order. The output I am getting is that the content of a post always sticks to whoever posts it, for example if I post something, then someone else posts something, then I post something again, it should be like this my post, their post, my post, instead it is like this, my post, my post, their post. So I am basically having an issue ordering what is coming out of the while loop. Here is the code I have.

<?php

require("scripts/connect.php");
session_start();
$my_id = $_SESSION["wave"]["id"];
$query = mysql_query("SELECT * FROM `follows` WHERE `follower_id` = '$my_id'")or die(mysql_error());
if(mysql_num_rows($query) >= 1)
{

    while($row = mysql_fetch_assoc($query))
    {   

        $following = $row["following_id"];
        $new_query = mysql_query("SELECT * FROM `posts` WHERE `user_id` = '$following' ORDER by `id` DESC");

        while($new_row = mysql_fetch_assoc($new_query))
        {


            echo $new_row["username"]."<br />".$new_row["content"]."<hr>";

        }

    }


}else
{

    echo "You are not following anyone; There are no new Ripples for you.";

}


?>
share|improve this question
    
order by dateandtime –  Feroz Akbar May 30 at 3:50
    
does your 'posts' table have a date submitted field you could sort by in the query? –  Nathaniel Wendt May 30 at 3:50
    
@NathanielWendt, no, I didn't think it would make much of a difference since the ID's are different by a space of 2 like ID 1 and 3, yet they still end up together. –  user3689853 May 30 at 3:52
    
if you dont have date and time field then order by id desc –  Feroz Akbar May 30 at 3:54
1  
The problem is you are doing queries in a loop. Use a join in the first query and order it there the way you want it. –  jeroen May 30 at 4:03
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.