Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:48:18+00:00 2026-05-26T23:48:18+00:00

I have the following code for displaying invites sent, received and accepted for a

  • 0

I have the following code for displaying invites sent, received and accepted for a simple friends system.

while ($friend = $q -> fetch(PDO::FETCH_ASSOC)) {
      if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have sent a request to be <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '\'s</a> friend.</p>';
      if ($friend['withID'] == $user['id'] && $friend['friendAccept'] == 0) echo '<p>You have recieved a request from <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '</a>.</p>';
      if ($friend['id'] == $user['id'] && $friend['friendAccept'] == 1) echo '<p>You are friends with <a href="http://www.n.com/viewaccount?id=' . $friend['withID'] . '">' . $friend['username'] . '</a>.</p>';
}

It will display what friends you have got, what friend requests you have sent, and what friend request have been received. I get all of this from one mysql query. But the only problem being they are not in order as the while loop iterates itself down the list and does whatever it is supposed to do.

Is there a way of getting them in order while still only using one query? By order I mean all the requests received in one list, requests sent in one list, and friends in one list. As it is at the minute they are in the order they are pulled from the table.

The query…

$q = $dbc -> prepare("SELECT social.*, accounts.username FROM social INNER JOIN accounts WHERE social.withID = accounts.ID AND (social.id = ? OR social.withID = ?) AND type = 'friend'");
$q -> execute(array($user['id'], $user['id']));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:48:19+00:00Added an answer on May 26, 2026 at 11:48 pm

    It’s absolutely possible:

    SELECT social.*, accounts.username 
    FROM social 
    INNER JOIN accounts 
    WHERE social.withID = accounts.ID 
    AND (social.id = ? OR social.withID = ?) 
    AND type = 'friend'
    ORDER BY 
    (social.id = $user_id AND social.friendAccept = 0), 
    (social.withID = $user_id AND social.friendAccept = 0),
    (social.id = $user_id AND social.friendAccept = 1)
    

    Just put the ORDER BY clause in the correct order. The way this works, it orders it using boolean values that the expressions evaluate to.

    Of course, you should also convert $user_id to ? in the prepared statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following css code for layout. td img {display: block;} While displaying images
I have code that looks like the following, which works fine for displaying the
Interesting one, this. I have the following JS code, which I'm displaying in a
I have the following code used for displaying several images changed on hover, but
I have the following code which keeps displaying 0 although rows are being inserted
I have added the following code for displaying a scrollbar to my textfield. But
I have the following code in Wordpress where I am displaying the meta values,
$message doesn't seem to be displaying anything. I have the following code: <?php $files
I have the following code, but the alert box is not displaying. try {
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.