0

So basically, i'm trying to make some sort of timeline with the post of my users, that gives me the posts of all users ordered by time. But instead, gives me the following:

*User1  'post1',
 User1  'post2',
 User1  'post3'..

*User2  'post1',
 User2  'post2',
 User2  'post3'..

I want all the post ordered instead of all the posts from user1 then all the posts from user2, etc. Well i hope it made sense thank you guys.

The query

  <?php
$varq1 = mysql_query('SELECT id2 FROM followers WHERE id ="'.$_SESSION['id'].'"');
    while($req5 = mysql_fetch_array($varq1)){

$req9=$req5['id2'];
$nsql = mysql_query('SELECT content, id, profile_id FROM contenido WHERE profile_id in("'.$req9.'") order by content_time desc');**strong text**
while($nrow = mysql_fetch_array($nsql)){    
?>

The HTML/ php call

<div class="divider">
<center><?php echo  "".$nrow['content'].""; ?><center/>
</div>
3
  • 2
    I really hope you've escaped everything you've put into that query with mysql_real_escape_string. There's a reason mysql_query is deprecated and should not be used in new applications, and it's that the interface is dangerous by default, prone to SQL injection bugs of the worst kind. Commented Nov 30, 2012 at 5:29
  • Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Nov 30, 2012 at 5:31
  • ok thank you guys, really informative stuff. Commented Nov 30, 2012 at 6:32

3 Answers 3

1

you can do both queries in a single one ... giving you the ability to sort by time

select content,id, profile_id from contenido where profile_id in (SELECT id2 FROM followers WHERE id ="'.$_SESSION['id'].'") order by content_time desc

fetch that and you will have exactly what you want :) all the posts from different users sorted by time.

Sign up to request clarification or add additional context in comments.

9 Comments

omg it worked, thank you!! sorry i can't give you a vote up, it says i need 15 rep, and this is my first question.
@Aaron , That's why you can accept the answer instead(do this by clicking the outlined checkmark under the down arrow). It gives more rep anyway.
Alright, but quick follow up question, what if i want to include the 'pictures' from another table into that query that you provided, lets say: mysql_query("SELECT reference,description FROM user_photos WHEREprofile_id='".$row['id']."' order by id desc");
In the above code i meant "order by time_pic". Any help is appreciated.
sorry for the late replay "i was sleeping :)" anyway its easy all you have to do is join 2 tables together.. in this way select contenido.content,contenido.id, contenido.profile_id from contenido join user_photos on contenido.profile_id = user_photos.profile_id where contenido.profile_id in (SELECT id2 FROM followers WHERE id ="'.$_SESSION['id'].'") order by contenido.content_time desc
|
0
'SELECT id2, content, c.id, profile_id
FROM followers f JOIN contenido c ON profile_id = id2
WHERE f.id = "'.$_SESSION['id']"'
ORDER BY content_time DESC, id2

Comments

0

Plz Use This Code Then Check

<?php
$varq1 = mysql_query('SELECT id2 FROM followers WHERE id ="'.$_SESSION['id'].'"');
while($req5 = mysql_fetch_array($varq1)){

$req9=$req5['id2'];
$nsql = mysql_query('SELECT content, id, profile_id FROM contenido WHERE profile_id='".$req9."' order by content_time desc');/*strong text*/
while($nrow = mysql_fetch_array($nsql)){    
?>
<div class="divider">
   <center><?php  '". echo $nrow['content']."'; ?><center/>
</div>

1 Comment

It didn't work as i wanted, but i guess i forgot to include important details, anyways thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.